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?
Asked
Active
Viewed 68 times
0
-
1Do you really *have* to use `Date`? – MC Emperor May 20 '21 at 22:06
1 Answers
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);

Alex Wesley
- 35
- 7
-
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. – Basil Bourque May 20 '21 at 23:56