dmitryvim
dmitryvim

Reputation: 2043

OffsetDateTime parsing

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

Upvotes: 13

Views: 65906

Answers (4)

muttonUp
muttonUp

Reputation: 6717

I had a slightly non-standard ISO(!) date to parse 2021-01-14T09:25:33+0000. The time is +0000 instead of +00:00

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

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.

Upvotes: 3

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79035

DateTimeFormatter need not be specified to parse the given string

You do not need a DateTimeFormatter to parse your date-time string, 2016-06-24T13:39:44.687680 into a LocalDateTime as it is already in the default format used by LocalDateTime#parse. Note that java.time API is based on ISO 8601 standards.

Demo:

import java.time.LocalDateTime;

public class Main {
    public static void main(String[] args) {
        LocalDateTime ldt = LocalDateTime.parse("2016-06-24T13:39:44.687680");
        System.out.println(ldt);
    }
}

Output:

2016-06-24T13:39:44.687680

How to parse a LocalDateTime string into OffsetDateTime?

You can create a DateTimeFormatter with the applicable ZoneId to parse a LocalDateTime string into ZonedDateTime which can in turn be converted into an OffsetDateTime.

Demo:

public class Main {
    public static void main(String[] args) {
        String strDateTime = "2016-06-24T13:39:44.687680";

        // DateTimeFormatter with system time zone
        DateTimeFormatter parserForDefaultTz = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault());
        OffsetDateTime odtSystemTz = ZonedDateTime.parse(strDateTime, parserForDefaultTz).toOffsetDateTime();
        System.out.println(odtSystemTz);

        // DateTimeFormatter with UTC time zone offset
        DateTimeFormatter parserForUtc = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.of("UTC"));
        OffsetDateTime odtUtc = ZonedDateTime.parse(strDateTime, parserForUtc).toOffsetDateTime();
        System.out.println(odtUtc);

        // For UTC it is even easier
        odtUtc = OffsetDateTime.parse(strDateTime, DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneOffset.UTC));
        System.out.println(odtUtc);
    }
}

Output:

2016-06-24T13:39:44.687680+01:00
2016-06-24T13:39:44.687680Z
2016-06-24T13:39:44.687680Z

However, a cleaner solution would be to parse a local date-time string into a LocalDateTime and convert it into an OffsetDateTime as described in this answer. Note that DateTimeFormatter.ISO_LOCAL_DATE_TIME is the default format used by LocalDateTime#parse. If your date-time string is in a custom format, create a corresponding DateTimeFormatter to use in this code.

Learn more about the modern Date-Time API from Trail: Date Time.

Upvotes: 0

Wilson
Wilson

Reputation: 11619

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

Upvotes: 25

Alexey Romanov
Alexey Romanov

Reputation: 170723

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.

Upvotes: 18

Related Questions