Locale.Builder setVariant() method in Java with Examples Last Updated : 12 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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 // 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- Comment More infoAdvertise with us Next Article Locale.Builder setRegion() method in Java with Examples C code_r Follow Improve Article Tags : Java Java - util package Java-Functions Java-Locale.Builder Practice Tags : Java Similar Reads Locale.Builder setRegion() method in Java with Examples The setRegion(String) method of java.util.Locale.Builder class in Java is used to set this Locale.Builder to the specified region. It means that this method will set the current region of Locale.Builder instance to match the provided region and return it. If the specified region is null or empty, th 2 min read Locale.Builder setScript() method in Java with Examples The setScript(String) method of java.util.Locale.Builder class in Java is used to set this Locale.Builder to the specified script. It means that this method will set the current script of Locale.Builder instance to match the provided script and return it. If the specified script is null or empty, th 2 min read 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 2 min read 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: publ 2 min read 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 ext 2 min read Locale.Builder setLocale(Locale) method in Java with Examples The setLocale(Locale) method of java.util.Locale.Builder class in Java is used to reset this Locale.Builder to the specified Locale. It means that this method will reset the current state of Locale.Builder instance to match the provided Locale and return it. Syntax: public Locale.Builder setLocale(L 2 min read Like