JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9

我是研究僧i 提交于 2019-11-26 09:56:09

问题


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);

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

mo = MONDAY

In Java 9, however it fails

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:

format = Mo

jdk-9 (build 9+181)

format = Mo.


回答1:


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

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.


And to second the data part of it, the international components for Unicode in German locale which has the following relevant information can justify that the behavior is intentional -

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

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.




回答2:


The abbreviatiions "Mo", "Di" etc. without dot have not disappeared in CLDR but are accessible via standalone-mode. You should change your pattern using the standalone format symbol "c" instead of "e":

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ccc", Locale.GERMAN);
DayOfWeek mo = dtf.parse("Mo", DayOfWeek::from);

Indeed, I consider the change of underlying data as breaking backwards compatibility (concrete as behavioural break).



来源:https://stackoverflow.com/questions/46244724/jdk-dateformatter-parsing-dayofweek-in-german-locale-java8-vs-java9

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!