Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7733635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:02:48+00:00 2026-06-01T07:02:48+00:00

I wrote a simple Util method to convert a String in Java to util.Date

  • 0

I wrote a simple Util method to convert a String in Java to util.Date. What I am not able to figure out is why the method works for the first input, and fails for the second one, given that the inputs are identical:

Code:

package util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class StringToDate {

    public Date getDateFromString(String strDate, String dateFormat) {

        DateFormat df = new SimpleDateFormat(dateFormat);
        Date date = null;

        try {

            date = df.parse(strDate);

        } catch (ParseException e) {
            e.printStackTrace();
        }

        System.out.println(date);

        return date;
    }

    public static void main(String[] args)  {

        StringToDate s2d = new StringToDate();

        s2d.getDateFromString("12-18-11, 10:36 AM","MM-dd-yy, hh:mm a");
        s2d.getDateFromString("02-04-12, 01:17 PM","MM-dd-yy, hh:mm a");
    }
}

Output:

Sun Dec 18 10:36:00 CET 2011
null
java.text.ParseException: Unparseable date: "02-04-12, 01:17 PM"
    at java.text.DateFormat.parse(DateFormat.java:337)
    at util.StringToDate.getDateFromString(StringToDate.java:17)
    at util.StringToDate.main(StringToDate.java:33)

Logically, the output should’ve been Sat Feb 04 13:17:00 CET 2012 going by the first output. Why is the ParseException being thrown?

EDIT: The following two lines work correctly:

s2d.getDateFromString("02-04-12", "MM-dd-yy");
s2d.getDateFromString("01:17 PM", "hh:mm a");

Output:

Sat Feb 04 00:00:00 CET 2012
Thu Jan 01 13:17:00 CET 1970

But the exception happens when I try to parse both date and time together.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T07:02:50+00:00Added an answer on June 1, 2026 at 7:02 am

    Do you have a non-breaking space, or some other Unicode space character, somewhere in either your date string or format mask?

    I was able to reproduce your error if I replaced one of the spaces in the second of your date strings with a non-breaking space, such as Unicode character 160.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to java.util.concurrent package and wrote a simple method which fetches some
I need to write a 'simple' util to convert from ASCII to EBCDIC? The
I wrote simple load testing tool for testing performance of Java modules. One problem
I wrote a simple Windows Forms program in C#. I want to be able
I am trying to write a simple script that convert the first field of
I'm trying to figure out a few simple best practices when it comes to
I recently found out about java.util.Properties , which allows me to write and read
I wrote an simple project using java rmi and exported to an executable jar
I wrote a simple batch file as a PowerShell script, and I am getting
I wrote a simple tool to generate a DBUnit XML dataset using queries that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.