-2
public static void main(String[] args) {
        Date now = new Date();

        String dateString = now.toString();
        System.out.println(" 1. " + dateString);

        SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");

        try {
            Date parsed = format.parse(dateString);
            System.out.println(" 2. " + parsed.toString());
        } catch (ParseException pe) {
            System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
        }

        System.out.println(" 3. " + format.format(now));
    }

print in console:

 1. Thu Feb 23 17:14:51 CET 2017
ERROR: Cannot parse "Thu Feb 23 17:14:51 CET 2017"
 3. jeu. févr. 23 05:14:51 CET 2017

instead of:

 1. Thu Feb 23 17:14:51 CET 2017
 2. Thu Feb 23 17:14:51 CET 2017
 3. jeu. févr. 23 05:14:51 CET 2017
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
  • 2
    Your no. 3 indicates your system locale is French, hence "Thu Feb" isn't recognized by the formatter as it expects French names only. – Thomas Feb 23 '17 at 16:23
  • 2
    Bonjour, is there a reason why you are still using the old-fashioned classes `Date` and `SimpleDateFormat`? The new classes in `java.time` (since Java 8) have `toString` and `parse` methods that agree about the default format and locale, so will save you of a problem like this one. – Ole V.V. Feb 23 '17 at 17:00

2 Answers2

3

It looks like your default Locale is FRENCH, and since SimpleDateFormat uses it if you don't specify another one, you can't successfully parse english abreviations like Thu or Feb.

However, you can specify the use of the ENGLISH Locale like this:

SimpleDateFormat format = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.ENGLISH);
Arnaud
  • 17,229
  • 3
  • 31
  • 44
0

You have run into one of the many diseases of the now out-dated Date class in Java.

The sound and easy fix is to change over to the Java 8 date and time classes. The new class that best corresponds to Date is Instant. With this class your code becomes:

    Instant now = Instant.now();

    String dateString = now.toString();
    System.out.println(" 1. " + dateString);

    try {
        Instant parsed = Instant.parse(dateString);
        System.out.println(" 2. " + parsed.toString());
    } catch (DateTimeParseException e) {
        System.out.println("ERROR: Cannot parse \"" + dateString + "\"");
    }

I have left out 3. from your example. Of course it is possible to format the Instant in about any way you could dream of, but if we just want the same output as from the toString method I don’t find it worth it for now. Rather I would like to show that for this case you don’t need a format at all. The code prints:

 1. 2017-02-23T17:07:19.775Z
 2. 2017-02-23T17:07:19.775Z

You notice that it prints the time in UTC. If you want your local time zone instead, just use ZonedDateTime instead of Instant. The rest of the code is exactly the same, but now output on my computer is:

 1. 2017-02-23T18:07:19.852+01:00[Europe/Berlin]
 2. 2017-02-23T18:07:19.852+01:00[Europe/Berlin]

Of course it is possible to generate an error like the one you get from disagreeing locales also with the new classes. As far as I can see, you would have to explicitly specify disagreeing locales. So you don’t do it easily.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161