3

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

Naman
  • 27,789
  • 26
  • 218
  • 353
Taka
  • 131
  • 1
  • 8
  • 1
    try SimpleDateFormat("yyyy-MMM-dd HH:mm:ss"); dateFormatGmt.setTimeZone(TimeZone.getTimeZone("GMT")); – Nigel Savage Dec 20 '19 at 22:36
  • 1
    Not too sure the cause of your issue but however you code appears to run on repl.it Take a look at [this](https://repl.it/repls/SneakySickDebugging) Also, probably not the cause of your issue but Monday wouldn't be the 19th of december – Andrew Rogers Dec 20 '19 at 23:04
  • 2
    see here `2019-dec-19` is `Thursday` but not monday, and also don't use `SimpleDateFormat` you can use newest datetime api from java `OffsetDateTime.parse("Thu, 19 Dec 2019 11:32:04 +0000",formatter)` – Ryuzaki L Dec 20 '19 at 23:08
  • Thank you very much, but I couldn't fix it. I used your ideas and except Nigels everything worked on repl.it [link](https://repl.it/repls/InsubstantialBackArchive) for more see original post – Taka Dec 21 '19 at 00:12
  • 1
    I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated, the former in particular notoriously troublesome. So throw away your first snippet and work on getting the second one to work. – Ole V.V. Dec 21 '19 at 14:03
  • 1
    Related: [java DateTimeFormatterBuilder fails on testtime](https://stackoverflow.com/questions/50526234/java-datetimeformatterbuilder-fails-on-testtime) (but do prefer the good answer below). – Ole V.V. Dec 21 '19 at 14:06

1 Answers1

5

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);
RubenDG
  • 1,365
  • 1
  • 13
  • 18
  • 2
    Very good answer. Very good working code and correct explanation of what went wrong in the question. – Ole V.V. Dec 21 '19 at 14:05