Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaLocale.Builder setLanguage() method in Java with Examples

Locale.Builder setLanguage() method in Java with Examples

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

Syntax:

public Locale.Builder
  setLanguage(String language)

Parameter: This method accepts the language as a parameter which is the String that is to be set to this Locale.Builder instance.

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

Exception: This method throws following Exceptions:

  • IllformedLocaleException: if the specified language 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 language of Locale.Builder
        String language = "FRENCH";
        System.out.println("Setting the language: "
                           + language);
  
        localeBuilder
            = localeBuilder.setLanguage(language);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}


Output:

LocaleBuilder: java.util.Locale$Builder@232204a1
Setting the language: FRENCH
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 language of Locale.Builder
        String language = "asdasf@!vsd#";
        System.out.println("Setting the language: "
                           + language);
          
        try{
        localeBuilder
            = localeBuilder.setLanguage(language);
  
        // 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 language: asdasf@!vsd#
java.util.IllformedLocaleException: Ill-formed language: asdasf@!vsd# [at index 0]

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

RELATED ARTICLES

Most Popular

Recent Comments