The java Calendar getDisplayName() method returns the string representation of the calendar field value in the given style and locale.
DeclarationFollowing 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
The method returns the string representation of the given field in the given style, or null if no string representation is available.
ExceptionIllegalArgumentException − 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
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): SeptemberGetting 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): SepGetting 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
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4