This question already has an answer here:
- Java string to date conversion 13 answers
How do I parse the date string below into a Date
object?
String target = "Thu Sep 28 20:29:30 JST 2000";
DateFormat df = new SimpleDateFormat("E MM dd kk:mm:ss z yyyy");
Date result = df.parse(target);
Throws exception...
java.text.ParseException: Unparseable date: "Thu Sep 28 20:29:30 JST 2000"
at java.text.DateFormat.parse(DateFormat.java:337)
This works fine?
Here is a working example:
Will print:
and
still runs. However, if your code throws an exception it is because your tool or jdk or any other reason. Because I got same error in my IDE but please check these http://ideone.com/Y2cRr (online ide) with ZZZ and with Z
output is :
Thu Sep 28 11:29:30 GMT 2000
The pattern is wrong. You have a 3-letter day abbreviation, so it must be
EEE
. You have a 3-letter month abbreviation, so it must beMMM
. As those day and month abbreviations are locale sensitive, you'd like to explicitly specify theSimpleDateFormat
locale to English as well, otherwise it will use the platform default locale which may not be English per se.This prints here
which is correct as per my timezone.
I would also reconsider if you wouldn't rather like to use
HH
instead ofkk
. Read the javadoc for details about valid patterns.A parse exception is a checked exception, so you must catch it with a try-catch when working with parsing Strings to Dates, as @miku suggested...
I had this issue, and I set the
Locale
toUS
, then it work.for
String
"Sun Jul 08 00:06:30 UTC 2012"