Java 日历 getDisplayName()方法及示例

Java 日历 getDisplayName()方法及示例

Calendar类的 getDisplayName(int cal_field , int cal_style , Locale local ) 方法用于返回一个日历字段(int cal_field)在给定风格(int cal_style)和locale(int local)中的字符串表示。

语法

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

参数: 该方法需要三个参数。

  • cal_field : 这是一个整数类型,指的是要对其进行操作的日历字段。
  • cal_style : 这是一个整数类型的参数,指的是要应用于字符串表示的样式。
  • local : 这是Locale对象的类型,指的是代表字符串的locale。

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

下面的程序说明了日历类的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://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getDisplayName(int, %20int, %20java.util.Locale)

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程