Java DateFormatSymbols setShortMonths()方法及实例

来源:这里教程网 时间:2026-02-17 20:45:15 作者:

Java DateFormatSymbols setShortMonths()方法及实例

Java中 DateFormatSymbols类setShortMonths(String[] newShMonth ) 方法是用来将日历中各月的短名称以字符串格式设置为一些不同的字符串。例如,”Jan “可以改成 “FEB”,”JUN “可以改成 “GEEK “等等。

语法

public void setShortMonths(String[] newShMonth)

参数: 该方法需要一个参数 newShMonth ,它是一个字符串类型的数组,指的是要在现有月份中替换的新字符串。

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

下面的程序说明了setShortMonths()方法的使用。

例1 :

// Java code to demonstrate setShortMonths()  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.getShortMonths();          // 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", "DEC",                             "NOV", "JAN",                             "FEB" };          // Setting the default into modified        format.setShortMonths(modDays);          // Displaying the modified string        String[] modifiedDays            = format.getShortMonths();          System.out.println("Modified: ");        for (int i = 0; i < modifiedDays.length; i++) {            System.out.println(modifiedDays[i] + "  ");        }    }}

输出:

Original: Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec  Modified: GEEK  FOR  GEEK  DEC  NOV  JAN  FEB

例2 :

// Java code to demonstrate setShortMonths()  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.getShortMonths();          // 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.setShortMonths(modDays);          // Displaying the modified string        String[] modifiedDays            = format.getShortMonths();          System.out.println("Modified: ");        for (int i = 0; i < modifiedDays.length; i++) {            System.out.println(modifiedDays[i] + "  ");        }    }}

输出:

Original: Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec  Modified: 123  456  JAN  FEB  NOV  Dec  May

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

相关推荐