java.time.ZonedDateTime.parse和iso8601吗? [英] java.time.ZonedDateTime.parse and iso8601?

查看:51
本文介绍了java.time.ZonedDateTime.parse和iso8601吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么JDK8 DateTime库似乎无法解析有效的iso8601日期时间字符串?它使时区偏移表示为"+01"而不是"+01:00".

Why does the JDK8 DateTime library seem to not parse valid iso8601 date time strings? It chokes on time zone offsets expressed like "+01" instead of "+01:00"

这有效:

java.time.ZonedDateTime.parse("2015-08-18T00:00+01:00")

这将引发解析异常:

java.time.ZonedDateTime.parse("2015-08-18T00:00+01")

从iso8601维基百科页面上:

From the iso8601 wikipedia page:

与UTC的偏移量以与"Z"相同的方式附加到时间上 上面的形式为±[hh]:[mm],±[hh] [mm]或±[hh].所以如果时间 被描述的时间比UTC早一小时(例如柏林的时间) 在冬季),区域标记将是"+01:00","+ 0100"或 只是"+01".

The offset from UTC is appended to the time in the same way that 'Z' was above, in the form ±[hh]:[mm], ±[hh][mm], or ±[hh]. So if the time being described is one hour ahead of UTC (such as the time in Berlin during the winter), the zone designator would be "+01:00", "+0100", or simply "+01".

编辑:这看起来像是JDK中的实际合法错误.

EDIT: This looks like an actual legitimate bug in the JDK.

https://bugs.openjdk.java.net/browse/JDK-8032051

哇,在测试了多年的新日期时间之后,我认为他们会发现如此明显的东西.我还认为JDK作者类型足够严格,可以使用更好的自动化测试套件.

Wow, after testing that new date time stuff for years, I thought they would have caught something so obvious. I also thought the JDK author types were rigorous enough to use a better automated test suite.

更新:在当前的jdk-9版本中,此问题已完全修复.我刚刚确认.上面显示的完全相同的parse命令在当前的jdk-8构建中失败,并且在jdk-9中可以完美地工作.

UPDATE: This is completely fixed in the current jdk-9 build. I just confirmed. The exact same parse command showed above fails in the current jdk-8 build and works perfectly in jdk-9.

附录:FWIW,基于ISO-8601的RFC 3339,不允许使用这种简写形式.您必须在时区偏移中指定分钟.

ADDENDUM: FWIW, RFC 3339 based on ISO-8601, does not allow for this short hand. You must specify minutes in the time zone offsets.

推荐答案

此问题在当前的jdk-9版本中已完全修复.我刚刚确认.上面显示的完全相同的parse命令在当前的jdk-8构建中失败,并且在jdk-9中可以完美地工作.

This is completely fixed in the current jdk-9 build. I just confirmed. The exact same parse command showed above fails in the current jdk-8 build and works perfectly in jdk-9.

使用新的jdk-9 shell:

Using the new jdk-9 shell:

➜  jdk-9 bin/jshell
|  Welcome to JShell -- Version 9-ea
|  For an introduction type: /help intro

jshell> java.time.ZonedDateTime.parse("2015-08-18T00:00+01")
$1 ==> 2015-08-18T00:00+01:00

这篇关于java.time.ZonedDateTime.parse和iso8601吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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