java.util.Calendar getDisplayName()方法

java.util.Calendar.getDisplayName() 方法返回以给定样式和本地形式作为参数传递的日历字段值的字符串表示形式。样式和局部也作为参数传递。如果没有可用的String表示形式,则getDisplayName() 返回空值。如果有可用String表示形式,则调用get()方法以访问日历字段值。

1 语法

public String getDisplayName(int field, int style, Locale locale)  

2 参数

field:日历字段传递如MONTH,DAY_OF_WEEK等

style:这是应用于作为参数传递的字段的String表示形式的样式。

locale:字符串表示形式的语言环境。

3 返回值

返回以指定格式(样式)和本地作为参数传递的字段的字符串表示形式。

4 示例1 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar getDisplayName()方法的例子
 */
import java.util.Calendar;  
import static java.util.Calendar.DAY_OF_WEEK;  
import static java.util.Calendar.LONG;  
import static java.util.Calendar.SATURDAY;  
import static java.util.Calendar.SUNDAY;  
import java.util.Locale;  
// first import all the required classes  
public class JavaCalendargetDisplayNameExample1 {  
    public static void main(String[] args) {  
        //getting the instance of calendar class using getinstance() method.  
      Calendar cal1 = Calendar.getInstance(new Locale("ja", "JP", "JP"));  
    Calendar cal2 = Calendar.getInstance(Locale.US);  
  
    // for loop starting from Sunday and running till Saturday  
    for (int i = SUNDAY; i <= SATURDAY; i++) {  
        cal1.set(DAY_OF_WEEK, i);  
         
  
        String c1 = cal1.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);  
        System.out.println(c1);  
           }  
}  
}   

输出结果为:

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday 

5 示例2

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar getDisplayName()方法的例子
 */
import java.util.Calendar;  
import static java.util.Calendar.DAY_OF_WEEK;  
import static java.util.Calendar.DECEMBER;  
import static java.util.Calendar.JANUARY;  
import static java.util.Calendar.LONG;  
import static java.util.Calendar.MONTH;  
import static java.util.Calendar.SATURDAY;  
import static java.util.Calendar.SUNDAY;  
import java.util.Locale;  
// first import all the required classes  
public class JavaCalendargetDisplayNameExample11 {      
    public static void main(String[] args) {  
        //getting the instance of calendar class using getinstance() method.  
      Calendar cal1 = Calendar.getInstance(new Locale("ja", "JP", "JP"));  
    Calendar cal2 = Calendar.getInstance(Locale.US);  
    // for loop starting from Sunday and running till Saturday\  
    System.out.println("Days of week are");  
         for (int i = JANUARY; i <= DECEMBER; i++) {  
        cal1.set(MONTH, i);  
        String c2 = cal1.getDisplayName(MONTH, LONG, Locale.US);  
        System.out.println(c2);  
    }  
    System.out.println("Months of year are");  
    for (int i = SUNDAY; i <= SATURDAY; i++) {    
       cal2.set(DAY_OF_WEEK, i);  
        String c1 = cal2.getDisplayName(DAY_OF_WEEK, LONG, Locale.US);  
        System.out.println(c1);  
              }  
}  
} 

输出结果为:

Days of week are
January
February
March
April
May
June
July
August
September
October
November
December
Months of year are
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday

6 示例3

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.util.Calendar getDisplayName()方法的例子
 */
import java.util.Calendar;  
import static java.util.Calendar.DAY_OF_YEAR;  
import static java.util.Calendar.LONG;  
import static java.util.Calendar.SATURDAY;  
import static java.util.Calendar.SUNDAY;  
import java.util.Locale;  
// first import all the required classes  
public class JavaCalendargetDisplayNameExample12 {  
        public static void main(String[] args) {  
        //getting the instance of calendar class using getinstance() method.  
      Calendar cal1 = Calendar.getInstance(new Locale("ja"));  
    Calendar cal2 = Calendar.getInstance(Locale.US);  
  
    // for loop starting from Sunday and running till Saturday  
    System.out.println("Days of year do not have String representation");  
    for (int i = SUNDAY; i <= SATURDAY; i++) {  
        cal1.set(DAY_OF_YEAR, i);  
       String c1 = cal1.getDisplayName(DAY_OF_YEAR, LONG, Locale.US);  
        System.out.println(c1);  
          
    }  
}  
}  

输出结果为:

Days of year do not have String representation
null
null
null
null
null
null
null

 

热门文章

优秀文章