Calendar提供了获取或设置各种日历字段的方法。
构造方法
其他方法
package com.cnblogs; import java.util.Calendar; import java.util.Date; //本类用于实现 public class Application { public static void main(String[] args) { Calendar calendar = Calendar.getInstance(); System.out.println(calendar.getTime().toLocaleString());//2021-7-15 8:12:23 System.out.println(calendar.getTimeInMillis());//1626307943057 //获取时间信息 int year = calendar.get(calendar.YEAR);//年 int month = calendar.get(calendar.MONTH);//月(0 - 11) int day = calendar.get(calendar.DAY_OF_MONTH);//日 int hour = calendar.get(calendar.HOUR_OF_DAY);//时 HOUR 12小时 HOUR_OF_DAY24小时 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,5); System.out.println(calendar2.getTime().toLocaleString());//2021-7-5 8:30:07 //add方法修改时间 calendar2.add(calendar2.HOUR,-1); System.out.println(calendar2.getTime().toLocaleString());//2021-7-5 7:46:21 //补充方法 int max = calendar2.getActualMaximum(calendar2.DAY_OF_MONTH); int min = calendar2.getActualMinimum(calendar2.DAY_OF_MONTH); System.out.println(max);//31 System.out.println(min);//1 } }