0

When I was trying to parse date in Java, both of these line return PhaseException, double-checked and doesn't know what' going on, please help!

Date dateobj = new SimpleDateFormat("MMM dd, yyyy").parse("Nov 12, 1994");
Date timeobj = new SimpleDateFormat("hh:mm a").parse("8:20 pm");

The full trace:

java.text.ParseException: Unparseable date: "Nov 12, 1994"
at java.text.DateFormat.parse(Unknown Source)
at testTime.main(testTime.java:12)
3
  • Are you catching or declaring that exception to be thrown? Commented Sep 6, 2014 at 6:53
  • @Makoto no, it is a exception declared by Java Commented Sep 6, 2014 at 6:54
  • 2
    PhaseException is not a standard Java exception. Post the full stack trace. Also, check that your Locale is English. All languages don't call November November. Commented Sep 6, 2014 at 7:01

3 Answers 3

3

First thing you code doesnot produces any Exception but still it is always safe to specify the Locale

Like this

Date dateobj = new SimpleDateFormat("MMM dd, yyyy", Locale.ENGLISH)
                                                          .parse("Nov 12, 1994");
Date timeobj = new SimpleDateFormat("hh:mm a", Locale.ENGLISH)
                                                     .parse("8:20 pm");
Sign up to request clarification or add additional context in comments.

Comments

0

Double check the declared throws for DateFormat#parse. It is throwing what's known as a checked exception; this means that even though this exception occurs, you should be able to recover from it.

You must either:

  • Declare it to be thrown (add throws ParseException at the signature of your method), or
  • Wrap the expression in a try...catch block. An example is below.

    Date dateobj = null;
    Date timeobj = null;
    try {
        dateobj = new SimpleDateFormat("MMM dd, yyyy").parse("Nov 12, 1994");
        timeobj = new SimpleDateFormat("hh:mm a").parse("8:20 PM");
    } catch(ParseException e) {
        e.printStackTrace();
    }
    

As it stands with your snippet, if I take either precaution (declaring it to be thrown or catch it myself), then I encounter no further runtime errors.

7 Comments

That's what I got java.text.ParseException: Unparseable date: "Nov 12, 1994" at java.text.DateFormat.parse(Unknown Source) at testTime.main(testTime.java:12)
Funny; I put your code into my IDE and I don't get that exception. That is, after I make sure to handle the exception properly.
I'm using Java 7. Did you try to use the above code in your program?
Yes, I use Eclipse with Java 7, your code gives the error msg listed in the main thread
Read my comment. In your locale, November is not called November. DateFormat uses your locale by default. Set its locale to English if you want to parse an English date.
|
0

Thanks to JB Nizet, this error is caused by a different locale setting, adding locale to the simpledateformat works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.