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?

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

6 Answers6

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);
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
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.

Rahul
  • 44,383
  • 11
  • 84
  • 103
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

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
1

Update the format as below :

SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
Kick
  • 4,823
  • 3
  • 22
  • 29
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

BackSlash
  • 21,927
  • 22
  • 96
  • 136
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.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110