Parsing Timezone String Date into Java's Instant

Viewed 346

I'm reading a from a few RSS sites which don't send the typical:

  • iso representation
    • 2019-06-12T07:17:47Z - Instant.parse() can be used
  • RFC1123
    • Wed, 12 Jun 2019 03:17:47 -0400 - DateTimeFormatter.RFC_1123_DATE_TIME.parse() can be used

Instead I'm getting these strings:

  • Tue, 25 May 2021 00:00:00 EDT
  • 03 Jun 2021 18:35:00 HKT

I've already tried around with some custom patterns and the ZonedDateTime + OffsetDateTime parse() method. Although I haven't found a way to get a date time representation that I can convert into Instant. Neither do I control the source and can fix the output format.

How can I be more lenient and parse these date times?

1 Answers

You can create a DateTimeFormatter with a custom pattern that has an optional day-of-week at the beginning. Afterwards, use the parse method of formatter with which you can specify the desired type of the parsed date-time directly (as per comment of Ole V.V.). Another approach is to first parse as ZonedDateTime and then convert to an Instant.

DateTimeFormatter formatter =
        DateTimeFormatter.ofPattern("[EEE, ]dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);

String input1 = "Tue, 25 May 2021 00:00:00 EDT";
Instant instant1 = formatter.parse(input1, Instant::from);
// Instant instant1 = ZonedDateTime.parse(input1, formatter).toInstant();
System.out.println(instant1);

String input2 = "03 Jun 2021 18:35:00 HKT";
Instant instant2 = formatter.parse(input2, Instant::from);
// Instant instant2 = ZonedDateTime.parse(input2, formatter).toInstant();
System.out.println(instant2);

Output:

2021-05-25T04:00:00Z
2021-06-03T10:35:00Z
Related