OffsetDateTime parsing

OffsetDateTime.parse requires a string containing an offset (+/-hh:mm), which "2011-12-03T10:15:30" doesn't have. Parse it with LocalDateTime.parse and convert the result using OffsetDateTime.of.


OffsetDateTime is a representation of a date-time with an offset. To create a OffsetDateTime, you need an zone offset.

A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.

see: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html

For example:

OffsetDateTime.parse("2011-12-03T10:15:30+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME);

If you try to parse a date time with ZoneId, you should use ZonedDateTime.

A date-time with a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00 Europe/Paris.

see: https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html

For example:

ZonedDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault()));

If what you need is a datetime without a time-zone in the ISO-8601 calendar system, you can use LocalDateTime.

A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

see: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html

For example:

LocalDateTime.parse("2016-06-24T13:39:44.687680", DateTimeFormatter.ISO_LOCAL_DATE_TIME);

Tags:

Datetime

Java