Home » Java programming language

Java Locale getDisplayVariant() Method with Example

Locale Class getDisplayVariant() method: Here, we are going to learn about the getDisplayVariant() method of Locale Class with its syntax and example.
Submitted by Preeti Jain, on March 08, 2020

Locale Class getDisplayVariant() method

Syntax:

    public final String getDisplayVariant();
    public String getDisplayVariant(Locale lo);
  • getDisplayVariant() method is available in java.util package.
  • getDisplayVariant() method is used to display name for the locale variant and the displayed name will be localized as per based on the default locale.
  • getDisplayVariant(Locale lo) method is used to display name for the locale variant and the displayed name will be localized as per based on the given locale (lo).
  • These methods may throw an exception at the time of displaying a variant of the locale.
    NullPointerException: This exception may throw when the given parameter is null exists.
  • These are non-static methods and it is accessible with the class object and if we try to access these methods with the class name then also we will get an error.

Parameter(s):

  • In the first case, getDisplayVariant() – It does not accept any parameter.
  • In the second case, getDisplayVariant(Locale lo)
    Locale lo – represents the locale.

Return value:

In both the cases, the return type of the method is String – it displays variant for the locale but does not return any value.

Example:

// Java program to demonstrate the example 
// of getDisplayVariant() method of Locale

import java.util.*;

public class GetDisplayVariantOfLocale {
    public static void main(String[] args) {
        // Instantiates Locale
        Locale lo1 = new Locale("JAPANESE", "JAPAN", "JAP");
        Locale lo2 = new Locale("FRENCH", "FRANCE", "FRA");

        // Display Locale
        System.out.println("lo1: " + lo1);
        System.out.println("lo2: " + lo2);

        // By using getDisplayVariant() method is
        // to return the name for this locale variant
        String var1 = lo1.getDisplayVariant();
        System.out.println("lo1.getDisplayVariant(): " + var1);

        // By using getDisplayVariant(locale) method is
        // to return the name for this locale variant will
        // be localized by the given locale
        String var2 = lo1.getDisplayVariant(lo2);
        System.out.println("lo1.getDisplayVariant(lo2): " + var2);
    }
}

Output

lo1: japanese_JAPAN_JAP
lo2: french_FRANCE_FRA
lo1.getDisplayVariant(): JAP
lo1.getDisplayVariant(lo2): JAP


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.