简体   繁体   English

为什么Java9中的时区WAT无法解析日期?

[英]Why is it unparsable date with timezone WAT in Java9?

SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
df.parse("Tue Nov 09 18:04:07 WAT 2021");

错误

Works fine with Java8.适用于 Java8。 But not with java9.但不是java9。 Using JDK 9.0.4使用 JDK 9.0.4


I am happy to get the alternate solutions.我很高兴获得替代解决方案。 But definitely existing applications breaks just upgrading to java 9.0.4.但肯定现有的应用程序只是升级到 java 9.0.4。 I would like to know the reasons for this and a way to handle without much change throughout the application.我想知道造成这种情况的原因以及在整个应用程序中没有太多变化的处理方法。

In the same online IDE, code works for java 9.0.1在同一在线 IDE 中,代码适用于 java 9.0.1

无处不在的中断 - Java 10

Working scenarios:工作场景:

爪哇 11

@Gibbs, indeed, it fails... @Gibbs,确实,它失败了......

I was unable to test the error with an installed JDK version, but I came across the online editor you showed in your screenshot.我无法使用已安装的 JDK 版本测试错误,但我遇到了您在屏幕截图中显示的在线编辑器

This will not be a complete answer but let me explain my findings.这不是一个完整的答案,但让我解释一下我的发现。

First, the problem is present in Java version 10.0.1.首先,问题出现在 Java 版本 10.0.1 中。 Using SimpleDateFormat or DateTimeFormatter makes no difference, so I will use the first one.使用SimpleDateFormatDateTimeFormatter没有区别,所以我将使用第一个。

Please, see the error screenshot:请看错误截图:

错误截图

My guess is that the error is related to an important change that was introduced in Java 9 regarding the use of CLDR locale data by default :我的猜测是,该错误与 Java 9 中引入的关于默认使用 CLDR 语言环境数据的重要更改有关:

In JDK 9, the default locale data uses data derived from the Unicode Consortium's Common Locale Data Repository (CLDR).在 JDK 9 中,默认语言环境数据使用从 Unicode 联盟的通用语言环境数据存储库 (CLDR) 派生的数据。 As a result, users may see differences in locale sensitive services behavior and/or translations.因此,用户可能会看到区域敏感服务行为和/或翻译的差异。 For example, CLDR does not provide localized display names for most 3- letter time zone IDs, thus the display names may be different from JDK 8 and older.例如,CLDR 不为大多数 3 字母时区 ID 提供本地化显示名称,因此显示名称可能与 JDK 8 和更早版本不同。 The JDK continues to ship with the legacy JRE locale data and the system property java.locale.providers can be used to configure the lookup order. JDK 继续提供旧版 JRE 语言环境数据,系统属性java.locale.providers可用于配置查找顺序。 To enable behavior compatible with JDK 8, the system property can be set with:要启用与 JDK 8 兼容的行为,可以使用以下方式设置系统属性:

-Djava.locale.providers=COMPAT,SPI

For more detail, refer to the JEP 252 .有关更多详细信息,请参阅JEP 252

As you can see, if you provide the suggested values for the java.locale.providers system property, note the change in the code, everything run smoothly:如您所见,如果您提供java.locale.providers系统属性的建议值,请注意代码中的更改,一切运行顺利:

成功执行

I think the error could be as well related to this and this other bugs.我认为该错误也可能与错误和其他错误有关。

So probably the error is related to the change to CLDR, but I am missing the reason why it starts failing (as far as understand, it should be since the first version of Java 9, but it seems to work with Java 9.0.1), and why it ends failing in Java 11 as well.所以错误可能与对 CLDR 的更改有关,但我错过了它开始失败的原因(据了解,它应该是从 Java 9 的第一个版本开始,但它似乎适用于 Java 9.0.1) ,以及为什么它在 Java 11 中也以失败告终。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM