当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。