Setting the Locale |
If you have not assigned aLocale
to a locale-sensitive object, it will rely on theLocale
returned by the methodLocale.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", "JP"); 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()); } }ja_JP fr_FR
Setting the Locale |