TimeZone getDisplayName(boolean, int) in Java

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

Syntax:

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

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

Parameters: Two 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.

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

Throws:

IllegalArgumentException - if the style is invalid.

Approach 1: When no exception

Java

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

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

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

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

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

Output:

GMT+00:00


Approach 2: IllegalArgumentException

Java

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

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

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

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

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

Output:

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


No comments:

Post a Comment