3

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?

2
  • Tue, 25 May 2021 00:00:00 EDT really conforms to RFC 1123 too. Only unfortunately Java’s implementation is limited: North American zone names … are not handled. (Doc) Commented Jun 6, 2021 at 17:09
  • 1
    @OleV.V. Alternative: My lib Time4J handles North American zone names. Commented Jun 7, 2021 at 10:50

1 Answer 1

5

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
Sign up to request clarification or add additional context in comments.

4 Comments

Good answer. Possible alternative: Instant instant1 = formatter.parse(input1, Instant::from); (same for input2). Feels a bit simpler to me, but it’s a matter of taste. Nice and clever use of optional part for the day of week (the square brackets)!
@OleV.V. Thanks for sharing your alternative which I personally find better since no additional conversion from ZonedDateTime to Instant is necessary. I updated my answer accordingly.
@Matt thanks a lot. Learned also about []. I've extended it to: [EEE, ]dd MMM yyyy HH:mm:ss zzz[Z] to be able to also parse dates such as Mon, 22 Mar 2021 18:39:18 GMT+0100. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.