📜  Java中的 Calendar getDisplayName() 方法及示例

📅  最后修改于: 2022-05-13 01:54:50.559000             🧑  作者: Mango

Java中的 Calendar getDisplayName() 方法及示例

Calendar 类的getDisplayName(int cal_field , int cal_style , Locale local )方法用于返回给定 style(int cal_style) 和 locale(int local) 中日历字段 (int cal_field) 值的字符串表示形式。

句法:

public String getDisplayName(int cal_field, int cal_style, Locale local)

参数:该方法接受三个参数:

  • cal_field :这是整数类型,指的是要执行操作的日历字段。
  • cal_style :这是整数类型,指的是应该应用于字符串表示的样式。
  • local :这是 Locale 对象类型,指的是表示字符串的语言环境。

返回值:该方法要么以传递样式的形式返回给定字段的字符串表示形式,否则如果没有字符串表示形式可用,则返回 null。

下面的程序说明了 Calendar 类的 getDisplayName() 方法的工作:
示例 1:

// Java Code to illustrate
// getDisplayName() Method
  
import java.util.*;
  
public class Calendar_Demo_Locale {
    public static void main(String args[])
    {
  
        // Creating Locale objects class
        Locale first_obj = new Locale("TURKISH", "Turkey");
  
        Locale sec_obj = new Locale("ENGLISH", "UK");
  
        // Displaying the objects
        System.out.println("First"
                           + " object is : " + first_obj);
        System.out.println("Second"
                           + " object is : " + sec_obj);
  
        // Getting the display names
        String obj_nm = first_obj.getDisplayName();
  
        // Displaying the results
        System.out.println("Name of the"
                           + " first object: " + obj_nm);
  
        // Getting the display names
        obj_nm = sec_obj.getDisplayName();
        System.out.println("Name of the"
                           + " second object: " + obj_nm);
    }
}
输出:
First object is : turkish_TURKEY
Second object is : english_UK
Name of the first object: turkish (TURKEY)
Name of the second object: english (UK)

示例 2:

// Java Code to illustrate
// getDisplayName() Method
  
import java.util.*;
  
public class Calendar_Demo_Locale {
    public static void main(String args[])
    {
  
        // Creating Locale objects class
        Locale first_obj = new Locale("RUSSIAN", "Russia");
  
        Locale sec_obj = new Locale("GERMAN", "Germany");
  
        // Displaying the objects
        System.out.println("First"
                           + " object is : " + first_obj);
        System.out.println("Second"
                           + " object is : " + sec_obj);
  
        // Getting the display names
        String obj_nm = first_obj.getDisplayName();
  
        // Displaying the results
        System.out.println("Name of the"
                           + " first object: " + obj_nm);
  
        // Getting the display names
        obj_nm = sec_obj.getDisplayName();
        System.out.println("Name of the"
                           + " second object: " + obj_nm);
    }
}
输出:
First object is : russian_RUSSIA
Second object is : german_GERMANY
Name of the first object: russian (RUSSIA)
Name of the second object: german (GERMANY)

参考: https: Java/util/Calendar.html#getDisplayName(int, %20int, %20java.util.Locale)