Java Set Default Locale

Ads
 

Java Set Default Locale

Locale can be set also in our java programs by using the method setDefault() of Locale class. In this example we have set the default locale value of our Locale with the Locale.ITALY.

Locale can be set also in our java programs by using the method setDefault() of Locale class. In this example we have set the default locale value of our Locale with the Locale.ITALY.

Java Set Default Locale

     

Setting Default Locale Example in Java

Locale can be set also in our java programs by using the method setDefault() of Locale class. In this example we have set the default locale value of our Locale with the Locale.ITALY.

In our example of setting default locale value we can get the value of default locale by using getDefault() method. Here it is English and now we have to change it with our Locale with the use of setDefault() method of Locale class. 

Locale locale = Locale.getDefault(); creates Locale class object by getting the default locale.

locale = Locale.ITALY;
Locale.setDefault(locale);

Above lines of code sets locale to ITALY and then sets the default locale value to this locale. Here is the full example code of SettingLocale.java as follows:

SettingLocale.java

import java.text.*;
import java.util.Locale;

public class SettingLocale {
  public static void main(String[] args) {
  // Getting default locale
  Locale locale = Locale.getDefault();
  System.out.println("Before setting, Locale is = " + locale);
  // Setting default locale  
  locale = Locale.ITALY;
  Locale.setDefault(locale);
  System.out.println("After setting, Locale is = " + locale);
 }
}

Output:

C:\DateExample>javac SettingLocale.java

C:\DateExample>java SettingLocale
Before setting, Locale is = en_US
After setting, Locale is = it_IT

Download Source Code

Ads

Ads