Java DateFormatSymbols getMonths()方法及示例

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

Java DateFormatSymbols getMonths()方法及示例

Java中 DateFormatSymbols类getMonths() 方法是用来获取字符串格式的日历月份名称的。

语法

public String[] getMonths()

参数: 该方法不接受任何参数。

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

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

例1 :

// Java code to demonstrate getMonths()  import java.text.DateFormatSymbols;  public class DateFormat_Main {    public static void main(String args[])    {        int i;          // Initializing DateFormatSymbols        String months[]            = new DateFormatSymbols().getMonths();          for (i = 0; i < months.length - 1; i++) {              // Displaying the months            System.out.println("Month = "                               + months[i]);        }    }}

输出:

Month = JanuaryMonth = FebruaryMonth = MarchMonth = AprilMonth = MayMonth = JuneMonth = JulyMonth = AugustMonth = SeptemberMonth = OctoberMonth = NovemberMonth = December

例2 :

// Java code to demonstrate getMonths()  import java.text.DateFormatSymbols;  public class DateFormat_Main {    public static void main(String args[])    {          int i;          // Initializing DateFormatSymbols        String months[]            = new DateFormatSymbols().getMonths();          for (i = 0; i < months.length / 2; i++) {              // Displaying the months            System.out.println("Month " + i                               + " = " + months[i]);        }    }}

输出:

Month 0 = JanuaryMonth 1 = FebruaryMonth 2 = MarchMonth 3 = AprilMonth 4 = MayMonth 5 = June

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

相关推荐