Setting the Locale |
If you have not assigned aLocale
to a locale-sensitive object, it will rely on theLocale
returned by the method getDefault. You can set the defaultLocale
in two ways:
- Set the
user.language
anduser.region
System properties
. TheLocale
class sets the default by retrieving the values of these properties.- Invoke the
setDefault
method.These two techniques for setting the default
Locale
are shown in the following example:The output of this program is as follows:import java.util.*; public class DefaultLocale { static public void main(String[] args) { Properties props = System.getProperties(); props.put("user.language", "ja"); props.put("user.region", "JA"); System.setProperties(props); Locale aLocale = Locale.getDefault(); System.out.println(aLocale.toString()); aLocale = new Locale("fr", "FR"); Locale.setDefault(aLocale); System.out.println(aLocale.toString()); } }Don't rely on the defaultja_JA fr_FRLocale
unless you set it ahead of time with one of the two methods shown above. If you don't, you may find that the theLocale returned by getDefault may not be the same on all Java platforms.
Setting the Locale |