How to parse this date in java?
Use a ZonedDateTime
.
ZonedDateTime t = ZonedDateTime.parse("2022-01-19T18:14:17+03:00");
This uses the default zone you are in (only the offset +03:00 is given. toString
is the inverse operation.
The format shown is that of an OffsetDateTime
, which needs some time zone, a (default) time zone ID, to have a full ZonedDateTime. Note that an OffsetDateTime is incomplete without the time zone. It migh be used to indicate a latitude on earth, but the local clocks might provide several differing times. Normally choose a ZonedDateTime.
The format of the input is the ISO standard with T
for time.
This means you do not need a DateTimeFormatter
.
If you need to use the obsolete old Date
:
Date date = Date.fromInstant(t.toInstant());