Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OffsetDateTime parsing

Tags:

java

datetime

Need to parse Date time from format 2016-06-24T13:39:44.687680.

First step using, tried to parse time without microseconds with line.

System.out.println(OffsetDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME));

Having exception

Exception in thread "main" java.time.format.DateTimeParseException: Text '2011-12-03T10:15:30' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
    at timeread.TimeReading.main(TimeReading.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.OffsetDateTime.from(OffsetDateTime.java:370)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 7 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {},ISO resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.ZoneOffset.from(ZoneOffset.java:348)
    at java.time.OffsetDateTime.from(OffsetDateTime.java:359)
    ... 9 more

Tried to use DateTimeFormatter with TimeZone

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

The similar exception

Exception in thread "main" java.time.format.DateTimeParseException: Text '2011-12-03T10:15:30' could not be parsed: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.format.DateTimeFormatter.createError(DateTimeFormatter.java:1920)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1855)
    at java.time.OffsetDateTime.parse(OffsetDateTime.java:402)
    at timeread.TimeReading.main(TimeReading.java:18)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.time.DateTimeException: Unable to obtain OffsetDateTime from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.OffsetDateTime.from(OffsetDateTime.java:370)
    at java.time.format.Parsed.query(Parsed.java:226)
    at java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1851)
    ... 7 more
Caused by: java.time.DateTimeException: Unable to obtain ZoneOffset from TemporalAccessor: {InstantSeconds=1322892930},ISO,Europe/Moscow resolved to 2011-12-03T10:15:30 of type java.time.format.Parsed
    at java.time.ZoneOffset.from(ZoneOffset.java:348)
    at java.time.OffsetDateTime.from(OffsetDateTime.java:359)
    ... 9 more
like image 755
dmitryvim Avatar asked Jun 27 '16 07:06

dmitryvim


3 Answers

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);
like image 187
Wilson Avatar answered Oct 10 '22 02:10

Wilson


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.

like image 42
Alexey Romanov Avatar answered Oct 10 '22 04:10

Alexey Romanov


I had a slightly non-standard ISO(!) date to parse 2021-01-14T09:25:33+0000

A standard DateTimeFormatter.ISO_OFFSET_DATE_TIME wouldn't handle it, but the DateTimeFormatterBuilder can be used to create one that would.

DateTimeFormatter customFormatter = new DateTimeFormatterBuilder()
   .append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
   .appendOffset("+HHMM","Z")
   .toFormatter();

The important bit is the .appendOffset() method, as using .ofPattern() doesn't seem to work with Offset parsing.

like image 1
muttonUp Avatar answered Oct 10 '22 02:10

muttonUp