How to change default locale settings in Java to make them consistent?

I am looking for a set of command line options that will allow me to force a specific set of locale settings for a Java application.

Let's say that I canโ€™t change the application, so I need to set the properties using java -Dmy.prop="value" .

I am looking specifically for changing the date / time format to make it compatible with ISO-8601 (wartime), but I could not find suitable properties for this.

+4
source share
3 answers

Nice, I was interested in the same thing, but so far I only have a partial solution :

 user.timezone=UTC user.country=CA user.variant= user.language=en file.encoding=utf8 

You can use Canada because it is one of the countries using the ISO 8601 format.

+1
source

I suspect that the correct solution to this problem would be to provide a new Locale option, such as (for example) en_US_mil .

 <language code>[_<country code>[_<variant code>]] 

This can be done by implementing classes through java.util.spi and java.text.spi .

This section is listed on the ICU website .

+3
source

The recommended way to set the standard Java language settings is to execute it through the shell locale / operating system settings. For example, on UNIX / LINUX, the default locale is determined by environment variables.

The following is information from the Java Internationalization FAQ :

Is it possible to set the default locale outside the application?

It depends on the implementation of the used Java platform. the default source standard is usually determined by the host system language. Sun JRE versions 1.4 and above allow you to override this by setting the user.language, user.country, and user.variant system properties from the command line. For example, to select Locale ("th", "TH", "TH") as the default initial locale, you should use:

java -Duser.language = th -Duser.country = TH -Duser.variant = TH MainClass

Since not all runtimes provide this feature, it should be for testing.

EDIT

The question asks:

I am looking specifically to change the date and time format to make it compatible with ISO-8601 (wartime), but I could not find the right tricks for this.

It looks like you want to selectively redefine some aspects of the current locale, rather than completely change it. An easy way to do this is to get the application to do the job; those. Use the specified date format, not the default format specified by the current locale. But if the application is closed, you may not be able to change it.

Perhaps you can get around this with the following approach:

  • Find out how Java implements the resolution of language triples (country / language / variant) for local objects.
  • For each locale that needs to be supported, subclasses of existing locale classes (etc.) to implement a new variant that uses ISO date and time formats.
  • Create a JAR containing your own classes or resources and add it to the class path. (You may need to add a JAR to the bootclasspath, etc., to make this work).

Finally, if the application does not use language mechanisms to determine which date format to use, any changes based on the language change will have no effect.

+2
source

All Articles