0

Trying to parse incoming dates from a third-party source, some dates work as expected others throw an exception:

Text 'Fri, 11 Jun 2021 02:25:23 +0000' could not be parsed at index 8

Looking at the dates I can't spot a difference in them, and looking at my formatter I can't see where I've gone wrong.

Example failing date: Fri, 11 Jun 2021 02:25:23 +0000
Example passing date: Sun, 30 May 2021 11:42:03 +0000

The code I'm using to parse the dates:

ZonedDateTime.parse(incomingDate, DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z"));

The only thing I can think that some date months are shorthand vs others that are not (May vs Jun for example).

Would love some help.

Chris
  • 3,437
  • 6
  • 40
  • 73
  • I've tried running your code locally and nothing is wrong with it. My output was correct for both dates you shared: `2021-06-11T02:25:23Z` and `2021-05-30T11:42:03Z`. Can you share what's failing? Erro and stacktrace perhaps? – Lucas Campos Jun 18 '21 at 08:31
  • 1
    Both work for me. Is your system language and region set to somewhere that doesn't call June "Jun"? If so, set the locale of the date formatter with `withLocale(Locale.ENGLISH)`. – Sweeper Jun 18 '21 at 08:32
  • [Never use SimpleDateFormat or DateTimeFormatter without a Locale](https://stackoverflow.com/a/65544056/10819573) – Arvind Kumar Avinash Jun 18 '21 at 08:46

1 Answers1

1
        String incomingDate1 = "Fri, 11 Jun 2021 02:25:23 +0000";
        String incomingDate2 = "Sun, 30 May 2021 11:42:03 +0000";

        ZonedDateTime parsed = ZonedDateTime.parse(incomingDate1,
                DateTimeFormatter.ofPattern("E, d MMM yyyy HH:mm:ss Z", Locale.ENGLISH));

        System.out.println(parsed);
Georgii Lvov
  • 1,771
  • 1
  • 3
  • 17
  • The Locale was the missing part... Odd though because I'm just in a generic US locale, nothing different about it. Thanks for this though – Chris Jun 18 '21 at 08:45
  • 1
    @ChrisTurner Try using your default locale to _format_ a date and see what `MMM` formats month 6 as. – Sweeper Jun 18 '21 at 08:47