Locale getDisplayVariant() inLocale in Java

getDisplayVariant(): This method is available in java.util.Locale class of Java.

Syntax:

String java.util.Locale.getDisplayVariant(Locale inLocale)

This method takes one argument of type Locale as its parameter. This method returns a name for the locale's variant code that is appropriate for display to the user.

Note: If the locale doesn't specify a variant code, this function returns the empty string.

Parameters: One parameter is required for this method.

inLocale: The locale for which to retrieve the display variant code.

Returns: The name of the display variant code appropriate to the given locale.

Throws: NullPointerException if inLocale is null.

Approach 1: When inLocale is not null.

Java

import java.util.Locale;

public class LocalegetDisplayVariant2 {
    public static void main(String[] args) {

        Locale locale = new Locale("en""USA""US");

        Locale inLocale = new Locale("fr""FR""FR");
        System.out.println(locale.getDisplayVariant(inLocale));
    }
}

Output:

US


Approach 2: When inLocale is null.

Java

import java.util.Locale;

public class LocalegetDisplayVariant2 {
    public static void main(String[] args) {

        Locale locale = new Locale("en""USA""US");

        Locale inLocale = null;
        System.out.println(locale.getDisplayVariant(inLocale));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "Object.hashCode()" because "key" is null at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) at java.base/sun.util.locale.provider.JRELocaleProviderAdapter.getLocaleResources(JRELocaleProviderAdapter.java:387) at java.base/java.util.Locale.getDisplayVariant(Locale.java:1960)


No comments:

Post a Comment