aj0822ArpitJoshi
aj0822ArpitJoshi

Reputation: 1182

Parse different date with same format

In Api response getting date format : **for user 1-

 "date": "2022-10-13T00:00:00+02:00[Africa/Johannesburg]".

for user 2-

"date": "2022-10-02T13:55:50.283+05:30[+05:30]**

How can we parse both date with single format?

Upvotes: 0

Views: 43

Answers (1)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59996

Both the dates are in format ISO_ZONED_DATE_TIME, so can parse them using ZonedDateTime:

ZonedDateTime user1 = ZonedDateTime.parse("2022-10-13T00:00:00+02:00[Africa/Johannesburg]");
ZonedDateTime user2 = ZonedDateTime.parse("2022-10-02T13:55:50.283+05:30[+05:30]");

Or if you want to use OffsetDateTime, then you have to use the DateTimeFormatter:

OffsetDateTime user1 = OffsetDateTime.parse(
        "2022-10-13T00:00:00+02:00[Africa/Johannesburg]",
        DateTimeFormatter.ISO_ZONED_DATE_TIME
);
OffsetDateTime user2 = OffsetDateTime.parse(
        "2022-10-02T13:55:50.283+05:30[+05:30]",
        DateTimeFormatter.ISO_ZONED_DATE_TIME
);

Upvotes: 3

Related Questions