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


Java DateFormatSymbols setMonths()用法及代码示例


Java中的DateFormatSymbols类的setMonths(String [] newMonth)方法用于以字符串格式将日历月份的名称设置为一些不同的字符串。例如,可以将“January”更改为“FEBRUARY”,将“JUNE”更改为“GEEK”等。

用法:

public void setMonths(String[] newMonth)

参数:该方法采用一个参数newMonth,该参数是String类型的数组,表示要在现有Months中替换的新字符串。


返回值:该方法以字符串格式返回修改后的月份名称。

以下示例程序旨在说明setMonths()方法的使用。
示例1:

// Java code to demonstrate setMonths() 
  
import java.text.DateFormatSymbols; 
import java.util.Locale; 
  
public class DateFormat_Main { 
    public static void main(String args[]) 
    { 
  
        // Initialising DateFormatSymbols object 
        DateFormatSymbols format 
            = new DateFormatSymbols( 
                new Locale("en", "US")); 
  
        // Taking the default short weekdays 
        String[] Days = format.getMonths(); 
  
        // Displaying the original 
        System.out.println("Original: "); 
        for (int i = 0; i < Days.length; i++) { 
            System.out.println(Days[i] + "  "); 
        } 
  
        // Taking an alternative names with 
        // additional random strings 
        String[] modDays = { "GEEK", "FOR", 
                             "GEEK", "DECEMBER", 
                             "NOVEMBER", "JAN", 
                             "FEB" }; 
  
        // Setting the default into modified 
        format.setMonths(modDays); 
  
        // Displaying the modified string 
        String[] modifiedDays 
            = format.getMonths(); 
  
        System.out.println("Modified: "); 
        for (int i = 0; i < modifiedDays.length; i++) { 
            System.out.println(modifiedDays[i] + "  "); 
        } 
    } 
}
输出:
Original: 
January  
February  
March  
April  
May  
June  
July  
August  
September  
October  
November  
December  
  
Modified: 
GEEK  
FOR  
GEEK  
DECEMBER  
NOVEMBER  
JAN  
FEB

示例2:

// Java code to demonstrate setMonths() 
  
import java.text.DateFormatSymbols; 
import java.util.Locale; 
  
public class DateFormat_Main { 
    public static void main(String args[]) 
    { 
  
        // Initialising DateFormatSymbols object 
        DateFormatSymbols format 
            = new DateFormatSymbols( 
                new Locale("en", "US")); 
  
        // Taking the default short weekdays 
        String[] Days = format.getMonths(); 
  
        // Displaying the original 
        System.out.println("Original: "); 
  
        for (int i = 0; i < Days.length; i++) { 
            System.out.println(Days[i] + "  "); 
        } 
  
        // Taking an alternative names with 
        // additional random strings 
        String[] modDays = { "123", "456", 
                             "JAN", "FEB", 
                             "NOV", "Dec", 
                             "May" }; 
  
        // Setting the default into modified 
        format.setMonths(modDays); 
  
        // Displaying the modified string 
        String[] modifiedDays 
            = format.getMonths(); 
  
        System.out.println("Modified: "); 
        for (int i = 0; i < modifiedDays.length; i++) { 
            System.out.println(modifiedDays[i] + "  "); 
        } 
    } 
}
输出:
Original: 
January  
February  
March  
April  
May  
June  
July  
August  
September  
October  
November  
December  
  
Modified: 
123  
456  
JAN  
FEB  
NOV  
Dec  
May

参考: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#setMonths-java.lang.String:A-



相关用法


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