java 偏移日期时间分析

wqsoz72f  于 2022-11-27  发布在  Java
关注(0)|答案(4)|浏览(148)

需要解析格式为2016-06-24T13:39:44.687680的日期时间。
第一步使用,尝试用行解析没有微秒的时间。

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

存在异常

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

尝试将DateTimeFormatterTimeZone一起使用

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

类似例外

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
kmpatx3s

kmpatx3s1#

OffsetDateTime是带偏移量的日期时间表示形式。要创建OffsetDateTime,需要区域偏移量。
在ISO-8601日历系统中,相对于UTC/格林威治有偏移量的日期时间,例如2007 - 12 - 03T10:15:30 + 01:00。
请参阅:https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
例如:

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

如果尝试使用ZoneId解析日期时间,则应使用ZonedDateTime
ISO-8601日历系统中带时区的日期时间,例如2007 - 12 - 03T10:15:30 + 01:00 Europe/Paris。
请参阅:https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
例如:

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

如果您需要的是ISO-8601日历系统中没有时区的日期时间,则可以使用LocalDateTime
ISO-8601日历系统中不带时区的日期时间,例如2007 - 12 - 03T10:15:30。
请参阅:https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
例如:

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

sqyvllje2#

OffsetDateTime.parse需要一个包含偏移量(+/-hh:mm)的字符串,而"2011-12-03T10:15:30"不具有该字符串。请使用LocalDateTime.parse解析该字符串,然后使用OffsetDateTime.of转换结果。

voase2hg

voase2hg3#

解析2021-01-14T09:25:33+0000时使用了一个稍微不标准的ISO(!)日期
标准的DateTimeFormatter.ISO_OFFSET_DATE_TIME不能处理它,但是DateTimeFormatterBuilder可以用来从现有的ISO格式创建一个可以处理它的DateTimeFormatterBuilder

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

最重要的是.appendOffset()方法,因为使用.ofPattern()似乎不适用于Offset解析。

dsf9zpds

dsf9zpds4#

不需要指定###DateTimeFormatter来分析给定的字符串
您不需要使用DateTimeFormatter将日期时间字符串2016-06- 24 T13:39:44.687680解析为LocalDateTime,因为它已经是LocalDateTime#parse使用的默认格式。请注意,java.time API基于ISO 8601标准。

演示

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

输出

2016-06-24T13:39:44.687680

如何将LocalDateTime字符串解析为OffsetDateTime

您可以使用适用的ZoneId建立DateTimeFormatter,将LocalDateTime字串剖析成ZonedDateTime,然后再将ZonedDateTime转换成OffsetDateTime

演示

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

输出

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

但是,一个更清晰的解决方案是将本地日期时间字符串解析为LocalDateTime,然后将其转换为OffsetDateTime,如this answer中所述。请注意,DateTimeFormatter.ISO_LOCAL_DATE_TIMELocalDateTime#parse使用的默认格式。如果您的日期时间字符串采用自定义格式,请创建相应的DateTimeFormatter以在此代码中使用。
从**Trail: Date Time**了解有关现代日期-时间API的更多信息。

相关问题