java - JDK 日期格式解析德语语言环境中的星期几,java 8 与 java 9

标签 java java-8 java-time java-9

我已经在 J​​ava 8 (1.8.0_77) 和 Java 9(Java HotSpot(TM) 64 位服务器虚拟机(构建 9+181,混合模式))中尝试了一些代码

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

我不太熟悉这些类的细节,但在 Java 8 中这是可行的,打印:

mo = MONDAY

在 Java 9 中,但是它失败了

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)

任何想法,这是可重现的吗?

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

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

jdk1.8.0-77:

format = Mo

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

format = Mo.

最佳答案

这似乎在 中由于当前实现 CLDR date-time-patterns随着 JEP - 252 的实现其中指出

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.

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


然后是它的数据部分,international components for Unicode in German locale具有以下相关信息可以证明该行为是故意的 -

enter image description here

编辑/注意:如评论中所链接,migration guide对此类实现发出类似警告-

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.

关于java - JDK 日期格式解析德语语言环境中的星期几,java 8 与 java 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46244724/

相关文章:

java - "context"在上下文(不)敏感的分析中到底意味着什么?

java - 组合数学算法(6选2)

java - 使用注解比接口(interface)类型有什么优势?

java - 如何从字符串 "06.03.2018 06:00 CET"获取 {Zoned,Offset}DateTime ?

java - _JAVA_OPTIONS 和 CLASSPATH 组合

java - 你能把一个流分成两个流吗?

java - 为什么并行 ForkJoinPool 加倍我的异常?

Java LocalDateTime 解析错误

java - JDBC 结果集检索 LocalDateTime

java - 专辑封面的 Glide 失败并出现 JNI 错误