JDK dateformatter在德语语言环境中解析DayOfWeek,java8 vs java9 [英] JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9

查看:102
本文介绍了JDK dateformatter在德语语言环境中解析DayOfWeek,java8 vs java9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了Java 8(1.8.0_77)和Java 9(Java HotSpot™64位服务器VM(内部版本9 + 181,混合模式))中的一些代码

I have tried some code in Java 8 (1.8.0_77) and Java 9 (Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode))

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("eee", Locale.GERMAN);
DayOfWeek mo = dtf.parse("Mo", DayOfWeek::from);
System.out.println("mo = " + mo);

我对这些类的细节不太熟悉,但是在Java 8中,可以打印以下内容:

I am not too familiar with details of those classes, but in Java 8 this works, printing:

mo =星期一

mo = MONDAY

在Java 9中,但是失败

In Java 9, however it fails

线程主"中的异常java.time.format.DateTimeParseException:无法在索引0处解析文本"Mo" 在java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988) 在java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1890) 在day.main(day.java:10)

Exception in thread "main" java.time.format.DateTimeParseException: Text 'Mo' could not be parsed at index 0 at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988) at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1890) at day.main(day.java:10)

任何想法,这是可复制的吗?

Any ideas, is this reproducible?

因此,格式化时: 使用此代码:

so, when formating: using this code:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("eee", Locale.GERMAN);
String format = dtf.format(DayOfWeek.MONDAY);
System.out.println("format = " + format);

jdk1.8.0-77:

jdk1.8.0-77:

format = Mo

format = Mo

jdk-9(内部版本9 + 181)

jdk-9 (build 9+181)

format = Mo.

format = Mo.

推荐答案

,因为 CLDR日期时间模式,其中包含 JEP-252 指出

This seems to be there in java-9 due to the current implementation of CLDR date-time-patterns with the implementation of JEP - 252 which states that

使用Unicode联盟的通用语言环境数据中的语言环境数据 默认情况下,存储库(CLDR).

Use locale data from the Unicode Consortium's Common Locale Data Repository (CLDR) by default.

用于显示格式和翻译的本地化模式 字符串(例如语言环境名称)在某些语言环境中可能会有所不同.

Localized patterns for the formatting and translation of display strings, such as the locale name, may be different in some locales.

要启用与JDK 8兼容的行为,请设置系统 属性java.locale.providers设置为带有 COMPAT且位于CLDR之前的值.

To enable behavior compatible with JDK 8, set the system property java.locale.providers to a value with COMPAT ahead of CLDR.


第二个数据部分是德语区域设置 中具有以下相关信息的Unicode国际组件可以证明该行为是故意的-

编辑/注释 :通过@ManiGrover链接,

Edit/Note: As linked by @ManiGrover, the migration guide states a similar warning for such implementations -

如果您的应用程序成功启动,请仔细检查您的测试 并确保其行为与JDK 8上的行为相同.例如, 很少有早期采用者注意到他们的日期和货币是 格式不同.请参见使用CLDR语言环境默认情况下为数据.

If your application starts successfully, look carefully at your tests and ensure that the behavior is the same as on JDK 8. For example, a few early adopters have noticed that their dates and currencies are formatted differently. See Use CLDR Locale Data by Default.

这篇关于JDK dateformatter在德语语言环境中解析DayOfWeek,java8 vs java9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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