Locale setDefault(Locale.Category, Locale) Method in Java with Examples

The setDefault(Locale.Category cate, Locale newLoc) method of Locale class in Java is used to set the default locale for this instance of the JVM or the Java Virtual machine and this in no way affects the locale host.

Syntax:

public static void 
    setDefault(Locale.Category cate,  
               Locale newLocale)

Parameters: The method takes two parameters:



  • cate: This is of Locale.Category type and specifies the category to which the default locale is to be set.
  • newLoc: This is also of locale type and refers to the new default locale.

Return Value: The method does not return any value.

Exception: The method can throw exceptions like-

  • SecurityException: This is thrown if a security manager exists and its checkPermission method doesn’t allow the operation.
  • NullPointerException: This is thrown if the newLoc is null.

Below programs illustrate the setDefault() Method of Locale class:
Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate setDefault() method
  
import java.util.*;
  
class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("nu", "NO", "NY");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Setting the Locale
        Locale.setDefault(Locale.Category.DISPLAY,
                          new Locale("ar", "SA"));
  
        Locale new_locale = Locale.getDefault();
  
        // Displaying the default locale of new locale
        System.out.println("The default locale: "
                           + new_locale);
    }
}

chevron_right


Output:

First Locale: nu_NO_NY
The Hash Code: en_US

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate setDefault() method
  
import java.util.*;
  
class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("en", "IN");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Setting the Locale
        Locale.setDefault(Locale.Category.FORMAT,
                          new Locale("en", "US"));
  
        Locale new_locale = Locale.getDefault();
  
        // Displaying the default locale of new locale
        System.out.println("The default locale: "
                           + new_locale);
    }
}

chevron_right


Output:

First Locale: en_IN
The Hash Code: en_US

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Locale.html#getDefault–



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.