Saturday, October 18, 2025
HomeLanguagesJavaLocale.Builder setLanguageTag() method in Java with Examples

Locale.Builder setLanguageTag() method in Java with Examples

The setLanguageTag(String) method of java.util.Locale.Builder class in Java is used to reset this Locale.Builder to the specified IETF BCP 47 language tag. It means that this method will reset the current state of Locale.Builder instance to match the provided language tag and return it.

Syntax:

public Locale.Builder setLanguageTag(String languageTag)

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

Return Value: This method returns an Locale.Builder instance which is the state of this Locale.Builder set to the specified language tag.

Exception: This method throws following Exceptions:

  • IllformedLocaleException: if the specified languageTag 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 languageTag
        // of Locale.Builder
        String languageTag = "FRENCH";
        System.out.println("Setting the language: "
                           + languageTag);
  
        localeBuilder
            = localeBuilder
                  .setLanguageTag(languageTag);
  
        // 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 languageTag
        // of Locale.Builder
        String languageTag = "asdasf@!vsd#";
        System.out.println("Setting the languageTag: "
                           + languageTag);
  
        try {
            localeBuilder
                = localeBuilder
                      .setLanguageTag(languageTag);
  
            // 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 languageTag: asdasf@!vsd#
java.util.IllformedLocaleException: Invalid subtag: asdasf@!vsd# [at index 0]

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

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS