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 Answer
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);
1 Comment
Basil Bourque
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.
Date?