JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9
我已经在Java 8(1.8.0_77)和Java 9(Java HotSpot(TM)64位服务器VM(内部版本9 + 181,混合模式))中尝试了一些代码
1 2 3 |
我对这些类的细节不太熟悉,但是在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)
任何想法,这是可复制的吗?
因此,格式化时:
使用此代码:
1 2 3 |
jdk1.8.0-77:
format = Mo
jdk-9(内部版本9 + 181)
format = Mo.
由于当前使用CLEP date-time-patterns以及JEP-252的实现,这似乎在java-9中存在。
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
propertyjava.locale.providers to a value with COMPAT ahead of CLDR.
其次是数据部分,德语区域的Unicode国际组件具有以下相关信息,可以证明该行为是故意的-
编辑/注释:正如注释中的链接所示,迁移指南针对此类实现提出了类似的警告-
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.
不带点的缩写" Mo"," Di"等在CLDR中并未消失,但可以通过独立模式访问。 您应该使用独立的格式符号" c"而不是" e"来更改模式:
1 2 | DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ccc", Locale.GERMAN); DayOfWeek mo = dtf.parse("Mo", DayOfWeek::from); |
实际上,我认为基础数据的更改是破坏向后兼容性(具体称为行为破坏)。