user2611714 user2611714 - 5 months ago 12
Java Question

Can't parse date format with SimpleDateFormat

I have a string "Thu Jan 01 01:32:49 EDT 1970" and trying to parse it with java.text.SimpleDateFormat

public class Main {
public static void main(String[] args) throws ParseException {
SimpleDateFormat sdf_date = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date d = sdf_date.parse("Thu Jan 01 01:32:49 EDT 1970");
System.out.println(d);
}
}


And get the exception:

Exception in thread "main" java.text.ParseException: Unparseable date: "Thu Jan 01 01:32:49 EDT 1970"
at java.text.DateFormat.parse(DateFormat.java:357)
at Main.main(Main.java:8)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)ppMain.main(AppMain.java:134)


I can't understand, what's wrong?

Answer

I think its a Locale issue, because the code worked for me. Try:

new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy",Locale.US);

DEMO