views:

64

answers:

3

I'm looking for a set of command line parameters that will allow me to force a specific set of locale settings for a java application.

Lets say that I'm not able to modify the application so I have to set the properties using java -Dmy.prop="value"

I'm looking specially to change the datetime format to make it ISO-8601 compliant (military time), but I was not able to find the proper proprieties for this.

A: 

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

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

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

Sorin Sbarnea
You wrote "partial" - what part of the problem does this *not* solve?
Thomas Mueller
it doesn't enforce a format for date/time
Maurice Perry
It should, but I tried it on hudson and it did not change it. Now depends on how the application is doing the formatting, you cannot expect miracles if the developer used hardcoded formatting.
Sorin Sbarnea
@Sorin Sbarnea - changing the JVM-specific `file.encoding` system property results in undefined behaviour: http://bugs.sun.com/view_bug.do?bug_id=4163515
McDowell
A: 

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

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

This can be done by implementing classes via the java.util.spi and java.text.spi service mechanisms.

This topic is mentioned on the ICU website.

McDowell
+2  A: 

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

The following information is from the Java Internationalization FAQ:

Can I set the default locale outside an application?

This depends on the implementation of the Java platform you're using. The initial default locale is normally determined from the host operating system's locale. Versions 1.4 and higher of Sun's JREs let you 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 initial default locale, you would use:

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

Since not all runtime environments provide this feature, it should only be used for testing.

EDIT

The question asks this:

I'm looking specially to change the datetime format to make it ISO-8601 compliant (military time), but I was not able to find the proper proprieties for this.

You appear to be want to selectively override certain aspects of the current locale, rather than change it entirely. The simple way to do this is to have the application do the work; i.e. use a specified date format rather than the default format given by the current locale. But if the application is closed source, you may not have the option of changing it.

It might be possible to work around this with the following approach:

  • Figure out how Java implements the resolution of locale triples (country/language/variant) to locale objects.
  • For each locale that you need to support, subclass the existing locale classes (etc) to implement a new variant that uses ISO date/time formats.
  • Create a JAR containing your custom classes or resources, and add it to the classpath. (You might need to add the JAR to the bootclasspath, etc to get this to work)

Finally, if the application is not using the locale mechanisms to decide what date format to use, any changes based on changing the locale will have no effect.

Stephen C
This still requires the JVM to support the locale: http://download.oracle.com/javase/6/docs/technotes/guides/intl/locale.doc.html#util-text
McDowell