需要从格式中解析日期时间2016-06-24T13:39:44.687680.
第一步使用,尝试用行解析没有微秒的时间.
System.out.println(OffsetDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME));
Run Code Online (Sandbox Code Playgroud)
有例外
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
Run Code Online (Sandbox Code Playgroud)
试着DateTimeFormatter用TimeZone
System.out.println(OffsetDateTime.parse("2011-12-03T10:15:30", DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneId.systemDefault())));
Run Code Online (Sandbox Code Playgroud)
类似的例外
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
Run Code Online (Sandbox Code Playgroud)
Wil*_*son 12
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);
Run Code Online (Sandbox Code Playgroud)
如果您尝试使用解析日期时间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()));
Run Code Online (Sandbox Code Playgroud)
如果您需要的是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);
Run Code Online (Sandbox Code Playgroud)
Ale*_*nov 10
OffsetDateTime.parse需要一个包含offset(+/-hh:mm)的字符串,该字符串"2011-12-03T10:15:30"没有.解析它LocalDateTime.parse并使用转换结果OffsetDateTime.of.
| 归档时间: |
|
| 查看次数: |
22352 次 |
| 最近记录: |