JDK dateformater解析德语语言环境中的DayOfWeek,java8 vs java9 [英] JDK dateformater parsing DayOfWeek in German locale, java8 vs java9
问题描述
我在Java 8(1.8.0_77)和Java 9(Java HotSpot(TM)64位服务器VM(构建9 + 181,混合模式))中尝试了一些代码。
DateTimeFormatter dtf = DateTimeFormatter.ofPattern(eee,Locale.GERMAN);
DayOfWeek mo = dtf.parse(Mo,DayOfWeek :: from);
System.out.println(mo =+ mo);
我对这些类的细节并不太熟悉,但在Java 8中这是有效的,打印:
mo =星期一
在Java 9中,但它失败
线程main中的异常java.time.format.DateTimeParseException:无法在索引处解析文本Mo 0
在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(build 9 + 181)
format = Mo。
这似乎出现在
编辑/注意 :由@链接ManiGrover,迁移指南对此类实施提出了类似的警告离子 -
如果您的应用程序成功启动,请仔细查看您的测试
,并确保行为与JDK上的行为相同8.例如,一些
的早期采用者注意到他们的日期和货币格式不同,为b $ b。请参阅使用CLDR区域设置默认数据。
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.
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.
这篇关于JDK dateformater解析德语语言环境中的DayOfWeek,java8 vs java9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!