2

A parser for email messages contains the following data format definition:

private final static DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");


if (line.startsWith("Date:")) {
        try {
            email.date = dateFormat.parse(line.substring(6));
        } catch (ParseException e) {
                System.err.println("Unparsable: " + line.substring(6));
        }

This code prints the error:

Unparsable: Mon, 15 Jan 2001 23:18:00 -0800 (PST)

Why do the formats not match?

EEE, d MMM yyyy HH:mm:ss Z
clstaudt
  • 19,377
  • 39
  • 142
  • 226
  • Does it work without the "(PST)" at the end? – Thilo Aug 06 '13 at 11:02
  • This seems to work `new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").parse("Mon, 15 Jan 2001 23:18:00 -0800 (PST)")` – Adam Siemion Aug 06 '13 at 11:04
  • If it's not the Locale problem pointed out by @JonSkeet, it could conceivably also be threading issues (SimpleDateFormat is not thread-safe), but that seems unlikely. – Thilo Aug 06 '13 at 11:07
  • [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash Jul 09 '21 at 17:43

2 Answers2

6

It works for me - but then I'm in an English locale to start with. That may be the problem - try explicitly specifying the locale when you construct the SimpleDateFormat:

private final static DateFormat dateFormat =
    new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);

Otherwise your current system locale will be used, and if that's not English it will be trying to parse different month and day names.

Jon Skeet
  • 1,335,956
  • 823
  • 8,931
  • 9,049
0
    DateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
    Date date=dateFormat.parse("Mon, 15 Jan 2001 23:18:00 -0800 (PST)");
    System.out.println(dateFormat.format(date));

I tried this and it pritns

   Tue, 16 Jan 2001 13:18:00 +0600

I am not getting any errors

Ruchira Gayan Ranaweera
  • 33,712
  • 16
  • 72
  • 110