Monday, June 15, 2026
HomeLanguagesJavaLocale.Builder setVariant() method in Java with Examples

Locale.Builder setVariant() method in Java with Examples

The setVariant(String) method of java.util.Locale.Builder class in Java is used to set this Locale.Builder to the specified variant. It means that this method will set the current variant of Locale.Builder instance to match the provided variant and return it. If the specified variant is null or empty, then the variant of this LocaleBuilder is removed. The specified variant is checked against the IETF BCP 47 variant subtag’s syntax requirements, after which is it normalized to lowercase.

Syntax:

public Locale.Builder setVariant(String variant)

Parameter: This method accepts the variant 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 variant set of this Locale.Builder to the specified variant.

Exception: This method throws following Exceptions:

  • IllformedLocaleException: if the specified variant 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 variant of Locale.Builder
        String variant
            = (new Locale("nu", "NO", "NY"))
                  .getDisplayVariant();
        System.out.println("Setting the variant: "
                           + variant);
  
        localeBuilder
            = localeBuilder.setVariant(variant);
  
        // Displaying Locale.Builder
        System.out.println("Updated LocaleBuilder: "
                           + localeBuilder);
    }
}


Output:

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

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

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32515 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6897 POSTS0 COMMENTS
Nicole Veronica
12013 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS