SimpleTimeZone getDisplayName(boolean, int) in Java

getDisplayName(boolean, int): This method is available in java.util.SimpleTimeZone 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 style is invalid.

Approach 1: When no exception

Java

import java.util.SimpleTimeZone;

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

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

        boolean daylight = true;
        int style = 1;

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

Output:

GMT+00:00


Approach 2: IllegalArgumentException 

Java

import java.util.SimpleTimeZone;

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

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

        boolean daylight = true;
        int style = -1;

        System.out.println(simpleTimeZone.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