0

I need to parse strings from the following type in Date objects in Java. "2021-05-19T20:43:29+00:00". I tried parsing it to "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" and "yyyy-MM-dd'T'HH:mm:ss.SSSZ" but both didn't work. I cannot find the specific format for this type. Can anyone help?

1
  • 1
    Do you really have to use Date? Commented May 20, 2021 at 22:06

1 Answer 1

1

I found that what works for me is the following

String dateFromAPI = "2021-05-19T20:43:29+00:00";
OffsetDateTime odt = OffsetDateTime.parse( dateFromAPI );
Instant instant = odt.toInstant();
Date date = Date.from(instant);
Sign up to request clarification or add additional context in comments.

1 Comment

Good Answer, except for that last line of code. java.util.Date class is terrible, flawed in both design and implementation. Sun, Oracle, and the JCP community all gave up on that class years ago with the unanimous adoption of JSR 310. I suggest you do the same.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.