📌  相关文章
📜  Java中的 DateFormatSymbols setMonths() 方法及示例

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

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: Java/text/DateFormatSymbols.html#setMonths-java.lang.String:A-