我已经在 Java 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.
最佳答案
这似乎在 java-9 中由于当前实现 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具有以下相关信息可以证明该行为是故意的 -
编辑/注意:如评论中所链接,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/