Question

I need to parse this string as a date:

Mon Jun 10 00:00:00 CEST 2013

Here is what I do:

SimpleDateFormat sdf = new SimpleDateFormat("ccc MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(dateString);

But I get a ParseException:

Unparseable date: "Wed Oct 02 00:00:00 CEST 2013" (at offset 0)

Any help please?

Was it helpful?

Solution

As others have said, you need EEE instead of ccc - but you should also specify a locale, so that it doesn't try to parse the month and day names (and other things) using your system default locale:

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

OTHER TIPS

Your format is wrong. You need to use EEE instead of ccc, where E means Day name in week.

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

Have a look at the docs regarding all the valid patterns available for SimpleDateFormat.

Replace ccc with EEE in the pattern to specify the day of the week:

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

Example: https://gist.github.com/kmb385/8781482

Update the format as below :

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");

It is a Locale problem. That's because dates are represented differently between Locales, so the JVM fires an exception if the Date is not in the correct format. You can solve it by setting a custom Locale:

String str = "Mon Jun 10 00:00:00 EST 2013";
Locale.setDefault(Locale.US);
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Date date = sdf.parse(str);
System.out.println(date);

IDEone examples do work because the default locale is Locale.US

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top