0

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?

3
  • 2
    Where does ccc come from? I suspect you want EEE. Commented Feb 3, 2014 at 10:13
  • @JonSkeet Probably it's a typo, using ccc would have fired a java.lang.IllegalArgumentException: Illegal pattern character 'c' Commented Feb 3, 2014 at 10:19
  • Oh, and you probably want to specify Locale.US (or something similar) - otherwise it'll be using your system default locale... Commented Feb 3, 2014 at 10:22

6 Answers 6

3

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);
Sign up to request clarification or add additional context in comments.

Comments

2

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.

3 Comments

I get the same exception :(
I'm sure it was a typo, as it would have fired java.lang.IllegalArgumentException: Illegal pattern character 'c' instead of a ParseException
@DanieleVitali - Working example. Are you sure you're using the exact same format?
2

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

3 Comments

I get the same exception :(
I'm sure it was a typo, as it would have fired java.lang.IllegalArgumentException: Illegal pattern character 'c' instead of a ParseException
@DanieleVitali Checkout this gist: gist.github.com/kmb385/8781482 Also could you post the full code for your example?
1

Update the format as below :

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

Comments

1

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

Comments

0

java.time

The accepted answer uses SimpleDateFormat which was the correct thing to do in Feb 2014. In Mar 2014, the java.util date-time API and their formatting API, SimpleDateFormat were supplanted by the modern Date-Time API. Since then, it is highly recommended to stop using the legacy date-time API.

Note: Never use date-time formatting/parsing API without a Locale.

Solution using the modern date-time API:

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

class Main {
    public static void main(String[] args) {
        String stdDateTime = "Mon Jun 10 00:00:00 CEST 2013";
        DateTimeFormatter parser = DateTimeFormatter.ofPattern("EEE MMM dd HH:mm:ss z uuuu", Locale.ENGLISH);
        ZonedDateTime zdt = ZonedDateTime.parse(stdDateTime, parser);
        System.out.println(zdt);
    }
}

Output:

2013-06-10T00:00+02:00[Europe/Paris]

If for any reason, you need an instance of java.util.Date, you can get it as follow:

Date date = Date.from(zdt.toInstant());

ONLINE DEMO

Learn more about the modern Date-Time API from Trail: Date Time.

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.