Java OffsetDateTime 解析
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38048032/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OffsetDateTime parsing
提问by dmitryvim
Need to parse Date time from format 2016-06-24T13:39:44.687680
.
需要从格式解析日期时间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
尝试DateTimeFormatter
与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
采纳答案by Alexey Romanov
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.parse
需要一个包含偏移量 ( +/-hh:mm
)的字符串,它"2011-12-03T10:15:30"
没有。解析它LocalDateTime.parse
并使用 转换结果OffsetDateTime.of
。
回答by Wilson
OffsetDateTime
is a representation of a date-time with an
offset.
To create a OffsetDateTime
, you need an zone offset.
OffsetDateTime
是具有偏移量的日期时间的表示。要创建OffsetDateTime
,您需要一个区域偏移量。
A date-time with an offset from UTC/Greenwich in the ISO-8601 calendar system, such as 2007-12-03T10:15:30+01:00.
在 ISO-8601 日历系统中与 UTC/格林威治有偏移的日期时间,例如 2007-12-03T10:15:30+01:00。
see: https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
见: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
.
如果您尝试使用 解析日期时间ZoneId
,则应使用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.
ISO-8601 日历系统中带有时区的日期时间,例如 2007-12-03T10:15:30+01:00 Europe/Paris。
see: https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
见: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
.
如果您需要的是 ISO-8601 日历系统中没有时区的日期时间,则可以使用LocalDateTime
.
A date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.
ISO-8601 日历系统中没有时区的日期时间,例如 2007-12-03T10:15:30。
see: https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
见: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);