Saturday, December 28, 2024
Google search engine
HomeLanguagesJavaLocale.Builder setExtension() method in Java with Examples

Locale.Builder setExtension() method in Java with Examples

The setExtension() method of java.util.Locale.Builder class in Java is used to set this Locale.Builder to the specified extension key. It means that this method will set the current extension of Locale.Builder instance to match the provided extension key and value and return it. If the specified extension is null or empty, then the extension of this LocaleBuilder is removed. The specified extension is checked against the LDML BCP 47 compatible extension.

Syntax:

public Locale.Builder setExtension(
                          char extensionKey, 
                          String extensionValue)

Parameter: This method accepts two parameters:

  • extensionKey: It is the character value that is to be set to this Locale>Builder instance.
  • extensionValue: It is the String value that is to be set to this Locale>Builder instance.

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

Exception: This method throws following Exceptions:

  • IllformedLocaleException: if the specified extension 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 extension of Locale.Builder
        char extensionKey = 'u';
        String extensionValue = "ca-japanese";
        System.out.println("Setting the extension: "
                           + extensionKey + "/"
                           + extensionValue);
  
        localeBuilder
            = localeBuilder
                  .setExtension(extensionKey,
                                extensionValue);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}


Output:

LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the extension: u/ca-japanese
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 extension of Locale.Builder
        char extensionKey = '@';
        String extensionValue = "fsdf#";
        System.out.println("Setting the extension: "
                           + extensionKey + "/"
                           + extensionValue);
  
        try {
  
            localeBuilder
                = localeBuilder
                      .setExtension(extensionKey,
                                    extensionValue);
  
            // 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 extension: @/fsdf#
java.util.IllformedLocaleException:
 Ill-formed extension key:
 @ [at index 0]

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

RELATED ARTICLES

Most Popular

Recent Comments