Locale getDisplayVariant(Locale inLoc) Method in Java with Examples

The getDisplayVariant(Locale inLoc) method of Locale class in Java is used to get the name for the locale’s variant code mentioned in the specified locale and is appropriately displayed to the user. If the variant code is not mentioned in the locale the function returns an empty string.

Syntax:

public String getDisplayVariant(Locale inLoc)

Parameters: This method takes one parameter inLoc of Locale type which refers to the localized display of the name.



Return Value: This method returns the name of the display language appropriate to the given locale.

Exceptions: The method throws NullPointerException if the parameter inLoc is null.

Below programs illustrate the working of getDisplayVariant() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate getDisplayVariant() method
  
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("th", "TH", "TH");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Displaying the variant code for locale
        System.out.println("Variant: "
                           + first_locale.getDisplayVariant(
                                 new Locale("fr", "French")));
    }
}

chevron_right


Output:

First Locale: th_TH_TH_#u-nu-thai
Variant: TH

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to illustrate getDisplayVariant() method
  
import java.util.*;
  
public class Locale_Demo {
    public static void main(String[] args)
    {
  
        // Creating a new locale
        Locale first_locale
            = new Locale("en", "US");
  
        // Displaying first locale
        System.out.println("First Locale: "
                           + first_locale);
  
        // Displaying the variant code for locale
        System.out.println("Variant: "
                           + first_locale.getDisplayVariant(
                                 new Locale("fr", "French")));
    }
}

chevron_right


Output:

First Locale: en_US
Variant:

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html#getDisplayVariant(java.util.Locale)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.