Vituel February 2016

What locale does DecimalFormat and DateFormat use by default? (why is it different to Locale.getDefault()?)

When debugging my application, if found that DecimalFormat and DateFormat are set to use nl_NL by default, but the system locale seems to be en_US.

I found the first by evaluating:

new DecimalFormat().symbols.locale
new SimpleDateFormat().locale

And the second:

Locale.getDefault()
System.getProperty("user.country")
System.getProperty("user.language")

What is happening there?

How can I set those formatters to use a certain locale by default? (I'm writing unit tests and would like to set a specific locale without touching the application code)

Answers


Vituel February 2016

As of Java 7, the default locale comes in two categories: "DISPLAY" and "FORMAT". Both are initially set according to the environment. They can be found by calling Locale.getDefault(Category).

Besides the system properties mentioned in the question:

"user.country"
"user.language"

The following can also be present when running the JVM:

"user.country.format"
"user.language.format"
"user.country.display"
"user.language.display"

The reason for that seems to be that some OS's allow independent localization settings for displaying text and for formatting dates, numbers and currency.

Answering my last question, by calling Locale.setDefault(Locale) the default locale will be set for all categories.

Post Status

Asked in February 2016
Viewed 3,140 times
Voted 5
Answered 1 times

Search




Leave an answer