Calendar
- Calendar提供了获取或设置各种日历字段的方法
- 构造方法
protected Calendar()
:由于修饰符是protected
,所以无法直接创建该对象。- 其他方法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5bad6a798685b3e7398067f8a0911ad8.png)
package com.jacyzhu.calendar;
import java.util.Calendar;
public class demo01 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year+"年"+(month+1)+"月"+day+"日"+hour+":"+minute+":"+second);
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH, 7);
System.out.println(calendar2.getTime().toLocaleString());
calendar2.add(Calendar.HOUR_OF_DAY, 1);
System.out.println(calendar2.getTime().toLocaleString());
calendar2.add(Calendar.MONTH, 1);
System.out.println(calendar2.getTime().toLocaleString());
int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(max);
System.out.println(min);
}
}
运行结果:
2022-8-8 17:04:22
1659949462551
2022年8月8日17:4:22
2022-8-7 17:04:22
2022-8-7 18:04:22
2022-9-7 18:04:22
30
1