使用 Java 日历显示月份


要使用日历类,请导入以下数据包。

import java.util.Calendar;

使用日历类创建对象。

Calendar calendar = Calendar.getInstance();

现在,创建月份名称的字符串数组。

String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" };

显示月份名称。

month[calendar.get(Calendar.MONTH)]

以下是一个示例。

示例

 实际演示

import java.util.Calendar;
public class Demo {
   public static void main(String[] args) {
      Calendar calendar = Calendar.getInstance();
      String[] month = new String[] {"January", "February", "March", "April", "May", "June", "July", "August","September", "October", "November", "December" };
      System.out.println("Current Month = " + month[calendar.get(Calendar.MONTH)]);
   }
}

输出

以下为输出 −

Current Month = Dec

更新于:2020 年 6 月 27 日

3K+ 浏览次数

Kickstart Your Career

完成课程取得认证

开始
广告