getDisplayNames(int, int, Locale): This method is available in java.util.GregorianCalendar class of Java.
Syntax:
Map<String, Integer> java.util.Calendar.getDisplayNames(int field, int style, Locale locale)
This method takes three arguments. 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.GregorianCalendar;import java.util.Locale;public class GregorianCalendargetDisplayNames {public static void main(String[] args) {GregorianCalendar cal = new GregorianCalendar();int field = 0, style = 1;Locale locale = Locale.US;System.out.println(cal.getDisplayNames(field,style, locale));}}
Output:
{AD=1, BC=0}
Approach 2: IllegalArgumentException
Java
import java.util.GregorianCalendar;import java.util.Locale;public class GregorianCalendargetDisplayNames {public static void main(String[] args) {GregorianCalendar cal = new GregorianCalendar();int field = -1, style = 1;Locale locale = Locale.US;System.out.println(cal.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.GregorianCalendar;import java.util.Locale;public class GregorianCalendargetDisplayNames {public static void main(String[] args) {GregorianCalendar cal = new GregorianCalendar();int field = 0, style = 1;Locale locale = null;System.out.println(cal.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