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
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);
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With