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


Java Calendar getDisplayName()用法及代码示例


Calendar类的getDisplayName(int cal_field,int cal_style,Locale local)方法用于以给定样式(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://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html#getDisplayName(int, %20int, %20java.util.Locale)



相关用法


注:本文由纯净天空筛选整理自Chinmoy Lenka大神的英文原创作品 Calendar getDisplayName() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。