Java.util.Calendar.getDisplayNames() Method


Description

The java.util.Calendar.getDisplayNames() method returns a Map containing all names of the calendar field in the given style and locale and their corresponding field values.

Declaration

Following is the declaration for java.util.Calendar.getDisplayNames() method

public Map<String,Integer> getDisplayNames(int field,int style,Locale locale)

Parameters

  • field − the calendar field.

  • style − the style that will be applied to the string representations

  • locale − the string representation locale

Return Value

The method returns a Map containing all display names in style and locale and their field values, or null if no string representation is available.

Exception

  • IllegalArgumentException − if field or style are invalid, or if this Calendar is non-lenient and any of the fields has invalid values

  • NullPointerException − if locale is null

Example

The following example shows the usage of java.util.calendar.getDisplayNames() method.

package com.tutorialspoint;

import java.util.*;

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

      // create calendar and locale
      Calendar now = Calendar.getInstance();
      Locale locale = Locale.getDefault();

      // call the getdisplaynames method
      Map< String, Integer> representations = 
      now.getDisplayNames(Calendar.DAY_OF_WEEK, Calendar.LONG, locale);
      NavigableMap< String, Integer> navMap =  
      new TreeMap< String, Integer>(representations);

      // print the results
      System.out.printf("Whole list:%n%s%n", navMap);
   }
}

Let us compile and run the above program, this will produce the following result −

Whole list:
{Monday=2, Sunday=1, Thursday=5, Friday=6, Saturday=7, Wednesday=4, Tuesday=3}
java_util_calendar.htm
Advertisements