简体   繁体   中英

Why is it unparsable date with timezone WAT in Java9?

SimpleDateFormat df = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy");
df.parse("Tue Nov 09 18:04:07 WAT 2021");

错误

Works fine with Java8. But not with java9. Using JDK 9.0.4


I am happy to get the alternate solutions. But definitely existing applications breaks just upgrading to java 9.0.4. I would like to know the reasons for this and a way to handle without much change throughout the application.

In the same online IDE, code works for java 9.0.1

无处不在的中断 - Java 10

Working scenarios:

爪哇 11

@Gibbs, indeed, it fails...

I was unable to test the error with an installed JDK version, but I came across the online editor you showed in your screenshot.

This will not be a complete answer but let me explain my findings.

First, the problem is present in Java version 10.0.1. Using SimpleDateFormat or DateTimeFormatter makes no difference, so I will use the first one.

Please, see the error screenshot:

错误截图

My guess is that the error is related to an important change that was introduced in Java 9 regarding the use of CLDR locale data by default :

In JDK 9, the default locale data uses data derived from the Unicode Consortium's Common Locale Data Repository (CLDR). As a result, users may see differences in locale sensitive services behavior and/or translations. For example, CLDR does not provide localized display names for most 3- letter time zone IDs, thus the display names may be different from JDK 8 and older. The JDK continues to ship with the legacy JRE locale data and the system property java.locale.providers can be used to configure the lookup order. To enable behavior compatible with JDK 8, the system property can be set with:

-Djava.locale.providers=COMPAT,SPI

For more detail, refer to the JEP 252 .

As you can see, if you provide the suggested values for the java.locale.providers system property, note the change in the code, everything run smoothly:

成功执行

I think the error could be as well related to this and this other bugs.

So probably the error is related to the change to CLDR, but I am missing the reason why it starts failing (as far as understand, it should be since the first version of Java 9, but it seems to work with Java 9.0.1), and why it ends failing in Java 11 as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM