當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java DateFormatSymbols setShortMonths()用法及代碼示例


Java中的DateFormatSymbols類的setShortMonths(String [] newShMonth)方法用於將字符串形式的日曆月份的簡稱設置為一些不同的字符串。例如,可以將“Jan”更改為“FEB”,將“JUN”更改為“GEEK”等。

用法:

public void setShortMonths(String[] newShMonth)

參數:該方法采用一個參數newShMonth,它是String類型的數組,表示要在現有Months中替換的新字符串。


返回值:該方法以字符串格式返回修改後的月份名稱。

以下示例程序旨在說明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-



相關用法


注:本文由純淨天空篩選整理自Chinmoy Lenka大神的英文原創作品 DateFormatSymbols setShortMonths() Method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。