TimeZone getDisplayName(boolean, int, Locale) in Java

getDisplayName(boolean, int, Locale): This method is available in java.util.TimeZone class of Java.

Syntax:

String java.util.TimeZone.getDisplayName(boolean daylight, int style, Locale locale)

This method takes three arguments. This method returns a name in the specified style of this TimeZone suitable for presentation to the user in the specified locale.

Parameters: Three parameters are required for this method.

daylight: true specifying a Daylight Saving Time name, or false specifying a Standard Time name.

style: either LONG or SHORT.

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

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

Throws:

1. IllegalArgumentException - if the style is invalid.

2. NullPointerException - if the locale is null.

Approach 1: When no exception

Java

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

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

        TimeZone timeZone = new SimpleTimeZone(100, "US");

        boolean daylight = true;
        int style = TimeZone.SHORT;

        Locale locale = Locale.US;

        System.out.println(timeZone.getDisplayName(
daylight, style, locale));
    }
}

Output:

GMT+00:00


Approach 2: IllegalArgumentException

Java

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

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

        TimeZone timeZone = new SimpleTimeZone(100, "US");

        boolean daylight = true;
        int style = TimeZone.SHORT - 1;

        Locale locale = Locale.US;

        System.out.println(timeZone.getDisplayName(
daylight, style, locale));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: Illegal style: -1 at java.base/java.util.TimeZone.getDisplayName(TimeZone.java:399)



Approach 3: NullPointerException

Java

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

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

        TimeZone timeZone = new SimpleTimeZone(100, "US");

        boolean daylight = true;
        int style = TimeZone.SHORT;

        Locale locale = null;

        System.out.println(timeZone.getDisplayName(
daylight, style, 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)


No comments:

Post a Comment