Calendar getDisplayNames(int, int, Locale) in Java

getDisplayNames(int, int, Locale): This method is available in java.util.Calendar class of Java.

Syntax:

Map<String, Integer> java.util.Calendar.getDisplayNames(int field, int style, Locale locale)

This method takes three arguments two are of type int and the other one of type Locale as its parameters. This method returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.

Parameters: Three parameters are required for this method.

field: the calendar field for which the display names are returned.

style: the style applied to the string representation; one of SHORT_FORMAT (SHORT), SHORT_STANDALONE, LONG_FORMAT (LONG), LONG_STANDALONE, NARROW_FORMAT, or NARROW_STANDALONE.

locale: the locale for the display names.

Returns: a Map containing all display names in style and locale and their field values, or null if no display names are defined for the field.

Throws:

1. IllegalArgumentException - if field or style is invalid, or if this Calendar is non-lenient and any of the calendar fields have invalid values.

2. NullPointerException - if the locale is null.

Approach 1: When no exception

Java

import java.util.Calendar;
import java.util.Locale;

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

        // create a calendar
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 2);

        int field = Calendar.MONTH, style = Calendar.ALL_STYLES;
        Locale locale = Locale.ENGLISH;
        System.out.println(calendar.getDisplayNames(field,
style, locale));
    }
}

Output:

{September=8, December=11, February=1, November=10,
January=0, October=9, August=7, April=3, March=2,
July=6, June=5, Apr=3, Aug=7, Dec=11, Feb=1,
Jan=0, Jul=6, Jun=5, Mar=2, May=4, Nov=10,
Oct=9, Sep=8}



Approach 2: IllegalArgumentException 

Java

import java.util.Calendar;
import java.util.Locale;

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

        // create a calendar
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 2);

        int field = -1, style = Calendar.ALL_STYLES;
        Locale locale = Locale.ENGLISH;
        System.out.println(calendar.getDisplayNames(field,
style, locale));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException at java.base/java.util.Calendar.checkDisplayNameParams(Calendar.java:2252) at java.base/java.util.Calendar.getDisplayNames(Calendar.java:2203)



Approach 3:NullPointerException

Java

import java.util.Calendar;
import java.util.Locale;

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

        // create a calendar
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, 2);

        int field = Calendar.MONTH, style = Calendar.ALL_STYLES;
        Locale locale = null;
        System.out.println(calendar.getDisplayNames(field,
style, locale));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Calendar.checkDisplayNameParams(Calendar.java:2255) at java.base/java.util.Calendar.getDisplayNames(Calendar.java:2203)



No comments:

Post a Comment