Open In App

Locale.Builder setUnicodeLocaleKeyword() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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:




// 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);
    }
}


Output:

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

Program 2:




// 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);
        }
    }
}


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-



Last Updated : 30 Jul, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads