當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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

參考: https://docs.oracle.com/javase/8/docs/api/java/time/DayOfWeek.html#getDisplayName-java.time.format.TextStyle-java.util.Locale-



相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 DayOfWeek getDisplayName() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。