JDK dateformatter 在德语语言环境中解析 DayOfWeek,java8 与 java9 [英] JDK dateformatter parsing DayOfWeek in German locale, java8 vs java9

查看:20
本文介绍了JDK dateformatter 在德语语言环境中解析 DayOfWeek,java8 与 java9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Java 8 (1.8.0_77) 和 Java 9 (Java HotSpot(TM) 64-Bit Server VM (build 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: 无法在索引 0 处解析文本Mo"在 java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:1988)在 java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1890)在 day.main(day.java:10)

任何想法,这是否可重现?

所以,在格式化时:使用此代码:

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("eee", Locale.GERMAN);字符串格式 = dtf.format(DayOfWeek.MONDAY);System.out.println("格式 = " + 格式);

jdk1.8.0-77:

<块引用>

格式 = 莫

jdk-9(构建 9+181)

<块引用>

格式 = 莫.

解决方案

编辑/注意:如评论中所链接,迁移指南 对此类实现提出了类似的警告 -

<块引用>

如果您的应用程序成功启动,请仔细查看您的测试并确保行为与 JDK 8 上的相同.例如,很少有早期采用者注意到他们的日期和货币是格式不同.请参阅 使用 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 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 in the comments, 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 dateformatter 在德语语言环境中解析 DayOfWeek,java8 与 java9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆