Java Calendar getDisplayName() Method



Description

The java Calendar getDisplayName() method returns the string representation of the calendar field value in the given style and locale.

Declaration

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

public String getDisplayName(int field,int style,Locale locale)

Parameters

  • field − the calendar field.

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

  • locale − the string representation locale

Return Value

The method returns the string representation of the given field in the given style, 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 Long Display Name of Month from a Calendar Instance Example

The following example shows the usage of Java Calendar getDisplayName() method. We're creating an instance of a Calendar of current date. Then month's display name is retrieved using getDisplayName() method and printed for two different locales and in long format.

package com.tutorialspoint;

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

public class CalendarDemo {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      
      // create objects of locale class
      Locale locale1 = new Locale("fr", "CA");
      Locale locale2 = new Locale("en", "GB");
      
      System.out.println("Display Name(fr): " 
         + cal.getDisplayName(Calendar.MONTH, Calendar.LONG_FORMAT, locale1));	
      System.out.println("Display Name(gb): "  
         + cal.getDisplayName(Calendar.MONTH, Calendar.LONG_FORMAT, locale2));	
   }
}

Output

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

Display Name(fr): septembre
Display Name(gb): September

Getting Short Display Name of Month from a Calendar Instance Example

The following example shows the usage of Java Calendar getDisplayName() method. We're creating an instance of a Calendar of current date. Then month's display name is retrieved using getDisplayName() method and printed for two different locales and in short format.

package com.tutorialspoint;

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

public class CalendarDemo {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      
      // create objects of locale class
      Locale locale1 = new Locale("fr", "CA");
      Locale locale2 = new Locale("en", "GB");
      
      System.out.println("Display Name(fr): " 
         + cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale1));	
      System.out.println("Display Name(gb): "  
         + cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, locale2));	
   }
}

Output

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

Display Name(fr): sept.
Display Name(gb): Sep

Getting Narrow Display Name of Month from a Calendar Instance Example

The following example shows the usage of Java Calendar getDisplayName() method. We're creating an instance of a Calendar of current date. Then month's display name is retrieved using getDisplayName() method and printed for two different locales and in narrow format.

package com.tutorialspoint;

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

public class CalendarDemo {
   public static void main(String[] args) {
      Calendar cal = Calendar.getInstance();
      
      // create objects of locale class
      Locale locale1 = new Locale("fr", "CA");
      Locale locale2 = new Locale("en", "GB");
      
      System.out.println("Display Name(fr): " 
         + cal.getDisplayName(Calendar.MONTH, Calendar.NARROW_FORMAT, locale1));	
      System.out.println("Display Name(gb): "  
         + cal.getDisplayName(Calendar.MONTH, Calendar.NARROW_FORMAT, locale2));	
   }
}

Output

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

Display Name(fr): S
Display Name(gb): S
java_util_calendar.htm
Advertisements