Calendar类的概述

  • Calendar 类是一个抽象类,它为特定瞬间与一组诸如 YEAR、MONTH、DAY_OF_MONTH、HOUR 等日历字段之间的转换提供了一些方法,并为操作日历字段(例如获得下星期的日期)提供了一些方法。

    成员方法

  • public static Calendar getInstance()

  • public int get(int field)
  1. public static void main(String[] args) {
  2. Calendar c = Calendar.getInstance(); //父类引用指向子类对象
  3. //System.out.println(c); //证明重写了toString方法打印了对象中所有的属性
  4. System.out.println(c.get(Calendar.YEAR)); //2021 //通过字段获取对应的值
  5. System.out.println(c.get(Calendar.MONTH));//8 月份是从0开始计算的,当前是9月份
  6. }