
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
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