java dayofweek_Java DayOfWeek getDisplayName()用法及代碼示例
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 dayofweek_Java DayOfWeek getDisplayName()用法及代碼示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 去停用词 java代码_如何在java中
- 下一篇: java动态添加组件_有关Java Sw