SimpleDateFormat causing unparseable error

213 Views Asked by At

I kinda used the following source to create my own sdf-pattern: https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/text/SimpleDateFormat.html

Unfortunately

    SimpleDateFormat mFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z");
    ...
    private Date getLatestTimeStamp() throws ParseException {
         return mFormatter.parse("Mon, 19 Dec 2019 11:32:04 +0000");
    }

causes the following error and I don't understand why:

java.text.ParseException: Unparseable date: "Mon, 19 Dec 2019 11:32:04 +0000"

Any help would be awesome!

EDIT: I am using JDK 13

EDIT 2:

I therefore cleaned up my code, created a new project but it still won't work:

import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;

public class Main {

    public static void main(String[] args) {

        String source = "Thu, 19 Dec 2019 11:32:04 +0000";
        DateTimeFormatter mFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z");

       System.out.println(OffsetDateTime.parse(source, mFormatter));
    }
}

Following the ful error message:

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Thu, 19 Dec 2019 11:32:04 +0000' could not be parsed at index 0 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2049) at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1951) at java.base/java.time.OffsetDateTime.parse(OffsetDateTime.java:402) at main.java.de.taka.main.Main.main(Main.java:15)

Process finished with exit code 1

1

There are 1 best solutions below

1
On BEST ANSWER

You should use RFC_1123_DATE_TIME formatter.

public static void main(String[] args) {
    String source = "Thu, 19 Dec 2019 11:32:04 +0000";
    DateTimeFormatter mFormatter = DateTimeFormatter.RFC_1123_DATE_TIME;

    System.out.println(OffsetDateTime.parse(source, mFormatter));
}

By the way, your pattern was good, you should just have added .withLocale().

DateTimeFormatter mFormatter = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss Z").withLocale(Locale.US);