Locale Updates in Java 21 - Sip of Java

Localization can often be challenging, with opportunities for subtle bugs to find their way into your applications. Luckily, JDK 21 saw several updates to this area that should help developers. Let’s take a look!

Looking up Default Locale and tzdata

When trying to resolve localization issues, one of the first steps would be to verify which Locale the JVM is using. This process has become easier with JDK 21 as the -X:showSettings option has been updated to include locale and tzdata. It can also be configured to only show locale data information with -X:showSettings:locale, like here:

$ java -X:showSettings:locale -version

Locale settings:
    default locale = English (United States)
    default display locale = English (United States)
    default format locale = English (United States)
    tzdata version = 2023c
    ...

Also shown are all available locales; however, as there are several hundred locales, that information is omitted from this article for readability.

See: JDK-8305950

Changing Default Locale

If you need to change the default Locale this can be done programmatically with the static factory methods Locale.of(), Locale.Builder, or Locale.forLanguageTag(), and passing the value to Locale.setDefault().

Swedish Language Updates

In 2006, Swedish sorting rules were changed to distinguish between the letters w and v. This change has been reflected in JDK-8306927.

Under the old rules, an array of {"vc", "va", "wb"} would be sorted as {"va", "wb", "vc"}, whereas under the new rules, it would be sorted as {"va", "vc", "wb"}.

If you want to use the old Swedish collation rules, this can be done by passing to sv-u-co-trad to Locale.forLanguageTag(String):

Locale TRADITIONAL_SWEDISH = Locale.forLanguageTag("sv-u-co-trad");

See: JDK-8306927

CLDR 43

Java uses CLDR for its locale data by default, which was updated to version 43 with JDK 21. The key change in this release was support for the coverageLevels.txt file.

See: JDK-8296248

Updating tzdata to 2023c

JDK 21 has been updated to use 2023c tzdata. This is effectively a rollback to 2023a due to changes in how Lebanon handles daylight savings time.

See: JDK-8305113

Additional Reading

2023c tzdata

Happy coding!