Home > Back-end >  Parse different date with same format
Parse different date with same format

Time:11-08

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?

CodePudding user response:

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
);

CodePudding user response:

you can not do that in one format parse difference date

  • Related