DayOfWeek getDisplayName() method in Java with Examples
The getDisplayName() method of java.time.DayOfWeek is an in-built function in Java which returns the textual representation of the day-of-week according to the specified Locale class parameter and TextStyle. The TextStyle defines three elements ‘FULL’, ‘SHORT’ and ‘NARROW’. Locale class represents a specific language and region of the world.
Method Declaration:
public String getDisplayName(TextStyle style, Locale locale)
Syntax:
String text = dayOfWeekObject.getDisplayName(TextStyle style, Locale locale)
Parameters: This method takes two parameters:
style – is the TestStyle which can be three elements ‘FULL’, ‘SHORT’ and ‘NARROW’.
locale – represents a specific language and region of the world. The default locale is US
dayOfWeekObject – is an instance of DayOfWeek.
Return Value: The function returns returns the textual representation of the day-of-week according to the specified Locale class parameter and TextStyle.
Below programs illustrate the above method:
Program 1:
import java.time.*;
import java.time.format.TextStyle;
import java.util.Locale;
class DayOfWeekExample {
public static void main(String[] args)
{
DayOfWeek dayOfWeek = DayOfWeek.MONDAY;
String full_name
= dayOfWeek
.getDisplayName(TextStyle.FULL,
Locale.getDefault());
String short_name
= dayOfWeek
.getDisplayName(TextStyle.SHORT,
Locale.getDefault());
String narrow_name
= dayOfWeek
.getDisplayName(TextStyle.NARROW,
Locale.getDefault());
System.out.println(full_name);
System.out.println(short_name);
System.out.println(narrow_name);
}
}
|
Program 2:
import java.time.*;
import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;
class DayOfWeekExample {
public static void main(String[] args)
{
DayOfWeek dayOfWeek = DayOfWeek.WEDNESDAY;
String full_name
= dayOfWeek
.getDisplayName(TextStyle.FULL,
Locale.getDefault());
String short_name
= dayOfWeek
.getDisplayName(TextStyle.SHORT,
Locale.getDefault());
String narrow_name
= dayOfWeek
.getDisplayName(TextStyle.NARROW,
Locale.getDefault());
System.out.println(full_name);
System.out.println(short_name);
System.out.println(narrow_name);
}
}
|
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#getDisplayName-java.time.format.TextStyle-java.util.Locale-
Last Updated :
20 Mar, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...