SimpleTimeZone getDisplayName(Locale) in Java

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

Syntax:

String java.util.TimeZone.getDisplayName(Locale locale)

This method takes one argument. This method returns a long standard time name of this TimeZone suitable for presentation to the user in the specified locale.

Parameters: One parameter is required for this method.

locale: the locale in which to supply the display name.

Returns: the human-readable name of this time zone in the given locale.

Throws:

NullPointerException - if locale is null.

Approach 1: When no exception

Java

import java.util.Locale;
import java.util.SimpleTimeZone;

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

        SimpleTimeZone simpleTimeZone =
new SimpleTimeZone(100, "India");

        Locale locale = Locale.US;
        System.out.println(simpleTimeZone.getDisplayName(
locale));
    }
}

Output:

GMT+00:00


Approach 2: NullPointerException 

Java

import java.util.Locale;
import java.util.SimpleTimeZone;

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

        SimpleTimeZone simpleTimeZone =
new SimpleTimeZone(100, "India");

        Locale locale = null;
        System.out.println(simpleTimeZone.getDisplayName(
locale));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/sun.util.locale.provider.LocaleServiceProviderPool.getLocalizedObjectImpl(LocaleServiceProviderPool.java:266) at java.base/sun.util.locale.provider.LocaleServiceProviderPool.getLocalizedObject(LocaleServiceProviderPool.java:236) at java.base/sun.util.locale.provider.TimeZoneNameUtility.retrieveDisplayNamesImpl(TimeZoneNameUtility.java:197) at java.base/sun.util.locale.provider.TimeZoneNameUtility.retrieveDisplayName(TimeZoneNameUtility.java:150) at java.base/java.util.TimeZone.getDisplayName(TimeZone.java:402) at java.base/java.util.TimeZone.getDisplayName(TimeZone.java:337)


No comments:

Post a Comment