OffsetDateTime解析 [英] OffsetDateTime parsing

查看:169
本文介绍了OffsetDateTime解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要从格式 2016-06-24T13:39:44.687680 解析日期时间。

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

有例外

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 with TimeZone

Tried to use DateTimeFormatter with 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


推荐答案

OffsetDateTime.parse 需要一个字符串包含ning偏移量( +/- hh:mm ),2011-12-03T10:15:30没有。使用 LocalDateTime.parse 解析它并使用 OffsetDateTime.of

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.

这篇关于OffsetDateTime解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆