本文介绍了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:

在Java 9中,但是失败

In Java 9, however it fails

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

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:

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

jdk-9 (build 9+181)

推荐答案

java-9 ,因为 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

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

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 dateformatter在德语语言环境中解析DayOfWeek,java8 vs java9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 04:53