1

I'm trying to parse date from string to Date My string date is: Fri Apr 30 01:20:29 +0700 2010

My code is:

SimpleDateFormat format = new SimpleDateFormat("E MMM dd HH:mm:ss Z yyyy");            
Date date = format.parse(input);

But i'm getting an Unparsable date exception. What's wrong?

7
  • 2
    Have you tried with EEE instead of E ? Commented Oct 19, 2015 at 15:48
  • Have a look at the examples @ docs.oracle.com/javase/6/docs/api/java/text/… Commented Oct 19, 2015 at 15:48
  • Possible duplicate of Java: unparseable date exception Commented Oct 19, 2015 at 15:48
  • Yes, i tried to use EEE instread of E. Unfortunately, catch exception too. Commented Oct 19, 2015 at 15:49
  • 1
    Hmm, just noticed the docs say "For parsing, both forms [long/short names] are accepted, independent of the number of pattern letters." Commented Oct 19, 2015 at 15:50

1 Answer 1

3

Check your format, you only have one E instead of three :

EEE MMM dd HH:mm:ss Z yyyy

EDIT : also check your JVM's language locale or specify one for your call.

As writen in comments, following code works :

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US);
Sign up to request clarification or add additional context in comments.

9 Comments

WTF is an "application lang"?!
I still wouldn't agree that there's something like an "application locale".
SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.US); works fine
I mean there's a system's default locale, which is what the application uses whenever it needs a locale and you don't provide one explicitly. But I may be nitpicking. Oracle says "The default locale is a systemwide resource, available throughout your application to any locale-sensitive object".
Calling Locale.setDefault() does not affect the host OS, but it does changes the current default for all code in all threads in all apps running in that same JVM. Changes on-the-fly at runtime, affecting all apps immediately. So you should almost never call this; do so only as a last resort. Ditto for TimeZone.setDefault. Instead, specify the desired/expected Locale or time zone as parameters passed to the methods. P.S. Use java.time instead of these tired old outmoded classes.
|

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.