Locale.Builder setUnicodeLocaleKeyword() method in Java with Examples

The setUnicodeLocaleKeyword() method of java.util.Locale.Builder class in Java is used to set this Locale.Builder to the specified unicodeLocaleKeyword key. It means that this method will set the current unicodeLocaleKeyword of Locale.Builder instance to match the provided unicodeLocaleKeyword key and type and return it. If the specified unicodeLocaleKeyword is null or empty, then the unicodeLocaleKeyword of this LocaleBuilder is removed.

Syntax:

public Locale.Builder
  setUnicodeLocaleKeyword(
    String unicodeLocaleKeywordKey, 
    String unicodeLocaleKeywordType)

Parameter: This method accepts two parameters:

  • unicodeLocaleKey: which is the key String that is to be set to this Locale.Builder instance for the unicode Locale
  • unicodeLocaleKeywordType: which is the type String that is to be set to this Locale.Builder instance for the unicode Locale

Return Type: This method returns an Locale.Builder instance with the unicodeLocaleKeyword set of this Locale.Builder to the specified unicodeLocaleKeyword.

Exception: This method throws following Exceptions:

  • IllformedLocaleException: if the specified unicodeLocaleKeyword has any ill formed fields

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleBuilderDemo {
    public static void main(String[] args)
    {
  
        // Creating a new Locale.Builder
        Locale.Builder localeBuilder
            = new Builder();
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // setting the unicodeLocaleKeyword
        // of Locale.Builder
        String unicodeLocaleKeywordKey = "nu";
        String unicodeLocaleKeywordType = "thai";
  
        System.out.println("Setting the "
                           + "unicodeLocaleKeyword: "
                           + unicodeLocaleKeywordKey + "-"
                           + unicodeLocaleKeywordType);
  
        localeBuilder
            = localeBuilder
                  .setUnicodeLocaleKeyword(
                      unicodeLocaleKeywordKey,
                      unicodeLocaleKeywordType);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}

chevron_right


Output:

LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the unicodeLocaleKeyword: nu-thai
Updated LocaleBuilder: java.util.Locale$Builder@232204a1

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// the above method
  
import java.util.*;
import java.util.Locale.*;
  
public class LocaleBuilderDemo {
    public static void main(String[] args)
    {
  
        // Creating a new Locale.Builder
        Locale.Builder localeBuilder
            = new Builder();
  
        // Displaying Locale.Builder
        System.out.println("LocaleBuilder: "
                           + localeBuilder);
  
        // setting the unicodeLocaleKeyword
        // of Locale.Builder
        String unicodeLocaleKeywordKey = "asf@";
        String unicodeLocaleKeywordType = "afaf$";
  
        System.out.println("Setting the "
                           + "unicodeLocaleKeyword: "
                           + unicodeLocaleKeywordKey + "-"
                           + unicodeLocaleKeywordType);
  
        try {
  
            localeBuilder
                = localeBuilder
                      .setUnicodeLocaleKeyword(
                          unicodeLocaleKeywordKey,
                          unicodeLocaleKeywordType);
  
            // Displaying Locale.Builder
            System.out.println("Updated LocaleBuilder: "
                               + localeBuilder);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the unicodeLocaleKeyword: asf@-afaf$
java.util.IllformedLocaleException:
 Ill-formed Unicode locale keyword key:
 asf@ [at index 0]

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Locale.Builder.html#setUnicodeLocaleKeyword-java.lang.String-java.lang.String-



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.