Java Calendar getDisplayNames() Method



Description

The Java 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

Getting Display Names of Days of Week from a Calendar Instance Example

The following example shows the usage of Java Calendar getDisplayNames() method. We're creating an instance of a Calendar of current date. Then we're creating a default locale. Using getDisplayNames(), we're getting all representations and printing

package com.tutorialspoint;

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

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);

      // print the results
      System.out.printf("Whole list: ", representations);
   }
}

Output

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}

Getting Display Names of Days of Week from a Calendar Instance for CA Locale Example

The following example shows the usage of Java Calendar getDisplayNames() method. We're creating an instance of a Calendar of current date. Then we're creating a locale for fr. Using getDisplayNames(), we're getting all representations and printing them.

package com.tutorialspoint;

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

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

      // create calendar and locale
      Calendar now = Calendar.getInstance();
      Locale locale = new Locale("fr", "CA");

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

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

Output

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

Whole list: {lundi=2, dimanche=1, vendredi=6, mercredi=4, jeudi=5, samedi=7, mardi=3}

Getting Display Names of Days of Week from a Calendar Instance for GB Locale Example

The following example shows the usage of Java Calendar getDisplayNames() method. We're creating an instance of a Calendar of current date. Then we're creating a locale for en. Using getDisplayNames(), we're getting all representations and printing them.

package com.tutorialspoint;

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

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

      // create calendar and locale
      Calendar now = Calendar.getInstance();
      Locale locale = new Locale("en", "GB");

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

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

Output

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