java.time.DayOfWeek的getDisplayName()方法是Java中的一個內置函數,它根據指定的Locale類參數和TextStyle返回day-of-week的文本表示形式。 TextStyle定義了三個元素“ FULL”,“ SHORT”和“ NARROW”。語言環境類別代表世界上特定的語言和地區。
方法聲明:
public String getDisplayName(TextStyle style, Locale locale)
用法:
String text = dayOfWeekObject.getDisplayName(TextStyle style, Locale locale)
參數:此方法有兩個參數:
- style–是TestStyle,可以包含三個元素“ FULL”,“ SHORT”和“ NARROW”。
- locale–代表世界上特定的語言和地區。默認語言環境是US
- dayOfWeekObject–是DayOfWeek的實例。
返回值:該函數返回根據指定的Locale類參數和TextStyle返回day-of-week的文本表示形式。
以下示例程序旨在說明上述方法:
示例1:
// Java Program Demonstrate getDisplayName()
// method of DayOfWeek
import java.time.*;
import java.time.format.TextStyle;
import java.util.Locale;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing a DayOfWeek instance
DayOfWeek dayOfWeek = DayOfWeek.MONDAY;
// Get textual representation of the
// day-of-week in FULL style
String full_name
= dayOfWeek
.getDisplayName(TextStyle.FULL,
Locale.getDefault());
// Get textual representation of the
// day-of-week in SHORT style
String short_name
= dayOfWeek
.getDisplayName(TextStyle.SHORT,
Locale.getDefault());
// Get textual representation of the
// day-of-week in NARROW style
String narrow_name
= dayOfWeek
.getDisplayName(TextStyle.NARROW,
Locale.getDefault());
// Printing the textual names of the day-of-week
System.out.println(full_name);
System.out.println(short_name);
System.out.println(narrow_name);
}
}
輸出:
Monday Mon M
示例2:
// Java Program Demonstrate getDisplayName()
// method of DayOfWeek
import java.time.*;
import java.time.DayOfWeek;
import java.time.format.TextStyle;
import java.util.Locale;
class DayOfWeekExample {
public static void main(String[] args)
{
// Initializing a DayOfWeek instance
DayOfWeek dayOfWeek = DayOfWeek.WEDNESDAY;
// Get textual representation of the
// day-of-week in FULL style
String full_name
= dayOfWeek
.getDisplayName(TextStyle.FULL,
Locale.getDefault());
// Get textual representation of the
// day-of-week in SHORT style
String short_name
= dayOfWeek
.getDisplayName(TextStyle.SHORT,
Locale.getDefault());
// Get textual representation of the
// day-of-week in NARROW style
String narrow_name
= dayOfWeek
.getDisplayName(TextStyle.NARROW,
Locale.getDefault());
// Printing the textual names of the day-of-week
System.out.println(full_name);
System.out.println(short_name);
System.out.println(narrow_name);
}
}
輸出:
Wednesday Wed W
相關用法
- Java TimeZone getDisplayName()用法及代碼示例
- Java Locale getDisplayName()用法及代碼示例
- Java ZoneOffset getDisplayName()用法及代碼示例
- Java ZoneId getDisplayName()用法及代碼示例
- Java Calendar getDisplayName()用法及代碼示例
- Java TimeZone getDisplayName(boolean, int)用法及代碼示例
- Java DayOfWeek of()用法及代碼示例
- Java DayOfWeek from()用法及代碼示例
- Java DayOfWeek plus()用法及代碼示例
- Java DayOfWeek get()用法及代碼示例
- Java DayOfWeek minus()用法及代碼示例
- Java DayOfWeek valueOf()用法及代碼示例
- Java DayOfWeek adjustInto()用法及代碼示例
- Java DayOfWeek getValue()用法及代碼示例
- Java DayOfWeek values()用法及代碼示例
注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 DayOfWeek getDisplayName() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。