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


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


Java中的DateFormatSymbols類的getShortMonths()方法用於以字符串格式獲取日曆月份的簡稱。

用法:

public String[] getMonths()

參數:該方法不帶任何參數。


返回值:該方法以字符串格式返回月份的簡稱。

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

// Java code to demonstrate getShortMonths() 
  
import java.text.DateFormatSymbols; 
  
public class DateFormat_Main { 
    public static void main(String args[]) 
    { 
  
        int i; 
  
        // Initializing DateFormatSymbols 
        String months[] 
            = new DateFormatSymbols() 
                  .getShortMonths(); 
  
        for (i = 0; i < months.length - 1; i++) { 
  
            // Displaying the short months 
            System.out.println( 
                "Month = " + months[i]); 
        } 
    } 
}
輸出:
Month = Jan
Month = Feb
Month = Mar
Month = Apr
Month = May
Month = Jun
Month = Jul
Month = Aug
Month = Sep
Month = Oct
Month = Nov
Month = Dec

示例2:

// Java code to demonstrate getShortMonths() 
  
import java.text.DateFormatSymbols; 
  
public class DateFormat_Main { 
    public static void main(String args[]) 
    { 
  
        int i; 
  
        // Initializing DateFormatSymbols 
        String months[] 
            = new DateFormatSymbols() 
                  .getShortMonths(); 
  
        for (i = 0; i < months.length / 2; i++) { 
  
            // Displaying the short months 
            System.out.println( 
                "Month " + i + " = " + months[i]); 
        } 
    } 
}
輸出:
Month 0 = Jan
Month 1 = Feb
Month 2 = Mar
Month 3 = Apr
Month 4 = May
Month 5 = Jun

參考: https://docs.oracle.com/javase/8/docs/api/java/text/DateFormatSymbols.html#getShortMonths–



相關用法


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