一、date
//构造方法:new一个对象
//当前日期
Date date = new Date();
//设置日期毫秒值
Date date1 = new Date(30000);
二、LocalDate
//静态方法
//获取当前日期
LocalDate nowDate = LocalDate.now();
//传参:年、月、日 获取LocalDate对象
LocalDate localdate = LocalDate.of(2024, 9, 7);
//传参:年、一年中第几天 获取LocalDate对象
LocalDate localdate1 = LocalDate.ofYearDay(2024,270);
//传参:一个日期(格式只能是"yyyy-MM-dd") 获取LocalDate对象!!!经常使用
LocalDate localdate2 = LocalDate.parse("2024-09-07");
//实例方法
//获取年
int year = localdate.getYear();
//获取月
Month month = localdate.getMonth();
//获取日
int dayOfMonth = localdate.getDayOfMonth();
//获取星期
DayOfWeek dayOfWeek = localdate.getDayOfWeek();
//获取某年的第几天
int dayOfYear = localdate.getDayOfYear();
//获取某月有几天
int d = localdate.lengthOfMonth();
//获取某年有几天
int m = localdate.lengthOfYear();
//localdate转字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String format = localdate.format(formatter);
//比较两个localdate的大小,返回相差的天数
int i = localdate.compareTo(nowDate);
//判断是否在某个日期之后 返回boolean
boolean after = localdate.isAfter(nowDate);
//判断是否在某个日期之前 返回boolean
boolean before = localdate.isBefore(nowDate);
//判断是否相等 返回boolean
boolean equal = localdate.isEqual(nowDate);
//判断是否是闰年
boolean leapYear = localdate.isLeapYear();
//减去年、月、日、周 (增加是plus)
LocalDate localDateMY = localdate.minusYears(1);
LocalDate localDateMM = localdate.minusMonths(1);
LocalDate localDateMD = localdate.minusDays(1);
LocalDate localDateMW = localdate.minusWeeks(1);
//调整到第几年、月、日
LocalDate localDateTY = localdate.withYear(2);
LocalDate localDateTM = localdate.withMonth(12);
LocalDate localDateTD = localdate.withDayOfMonth(30);
三、Calendar
//静态方法
//获取日历对象
Calendar instance = Calendar.getInstance();
Calendar instance1 = Calendar.getInstance();
//设置日历字段的值
instance.set(2024,9,7);
instance1.set(2024,9,6);
//获取日历字段的值
instance.get(Calendar.YEAR);
//日历比较
boolean before1 = instance.before(instance1);
boolean after1 = instance.after(instance1);
int i1 = instance.compareTo(instance1);