Java Locale setDefault() Method



Description

The Java Locale setDefault(Locale newLocale) method sets the default locale for this instance of the Java Virtual Machine. This does not affect the host locale.

Declaration

Following is the declaration for java.util.Locale.setDefault() method

public static void setDefault(Locale newLocale)

Parameters

newLocale − the new default locale

Return Value

This method returns a hash code value for this object.

Exception

  • SecurityException − if a security manager exists and its checkPermission method doesn't allow the operation.

  • NullPointerException − if newLocale is null

Setting Default Locale as CANADA Example

The following example shows the usage of Java Locale setDefault() method. We're getting a default locale, printing it. Then using setDefault() method, we're updating it and then default locale is printed.

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // print this locale
      System.out.println("Locale:" + Locale.getDefault());

      // Set the default Locale
      Locale.setDefault(Locale.CANADA_FRENCH);

      System.out.println("Locale:" + Locale.getDefault());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Locale:en_IN
Locale:fr_CA

Setting Default Locale as FRANCE Example

The following example shows the usage of Java Locale setDefault() method. We're getting a default locale, printing it. Then using setDefault() method, we're updating it and then default locale is printed.

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // print this locale
      System.out.println("Locale:" + Locale.getDefault());

      // Set the default Locale
      Locale.setDefault(Locale.FRANCE);

      System.out.println("Locale:" + Locale.getDefault());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Locale:en_IN
Locale:fr_FR

Setting Default Locale as US Example

The following example shows the usage of Java Locale setDefault() method. We're getting a default locale, printing it. Then using setDefault() method, we're updating it and then default locale is printed.

package com.tutorialspoint;

import java.util.Locale;

public class LocaleDemo {
   public static void main(String[] args) {

      // print this locale
      System.out.println("Locale:" + Locale.getDefault());

      // Set the default Locale
      Locale.setDefault(Locale.US);

      System.out.println("Locale:" + Locale.getDefault());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Locale:en_IN
Locale:en_US
java_util_locale.htm
Advertisements