需要从格式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
尝试将DateTimeFormatter
与TimeZone
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
答案 0 :(得分:11)
OffsetDateTime
表示日期时间
偏移。
要创建OffsetDateTime
,您需要一个区域偏移量。
ISO-8601日历中与UTC / Greenwich偏移的日期时间 系统,如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欧洲/巴黎。
请参阅: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);
答案 1 :(得分:9)
OffsetDateTime.parse
需要一个包含偏移量(+/-hh:mm
)的字符串,"2011-12-03T10:15:30"
没有。使用LocalDateTime.parse
解析它并使用OffsetDateTime.of
转换结果。
答案 2 :(得分:1)
我有一个稍微不标准的 ISO(!) 日期来解析 2021-01-14T09:25:33+0000
标准的 DateTimeFormatter.ISO_OFFSET_DATE_TIME
不会处理它,但 DateTimeFormatterBuilder 可用于创建一个。
DateTimeFormatter customFormatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.ISO_LOCAL_DATE_TIME)
.appendOffset("+HHMM","Z")
.toFormatter();
重要的一点是 .appendOffset()
方法,因为使用 .ofPattern()
似乎不适用于偏移解析。