1、LocalDate相关操作:
日期比较大小用compareTo
int result = a.compareTo(b);
result = 0 : a = b
result = 1:a > b
result = -1:a < b
LocalDate date = LocalDate.now();
LocalDateTime time = LocalDateTime.now();
DateTimeFormatter dtf0 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern("yyyy-MM");
DateTimeFormatter dtf3 = DateTimeFormatter.ofPattern("yyyy");
String date1 = date.format(dtf1); //2023-06-01
String date2 = date.format(dtf2); //2023-06
String date3 = date.format(dtf3); //2023
String time0 = time.format(dtf0); //2023-06-01 20:13:51
String time1 = time.format(dtf1); //2023-06-01
String time2 = time.format(dtf2); //2023-06
String time3 = time.format(dtf3); //2023
//格式化年月
YearMonth yearMonth = YearMonth.parse("2023-02", dtf2); //2023-02
//获取当前月的最后一天
LocalDate currentDate = yearMonth.atEndOfMonth(); //2023-02-28
//格式化为String
String format = currentDate.format(dtf1); //2023-02-28
//获取当前月的第一天
LocalDate firstDay = currentDate.with(TemporalAdjusters.firstDayOfMonth()); //2023-02-01
//获取当前月下月的第一天
LocalDate nextMonthFirstDay = firstDay.minusMonths(-1); //2023-03-01
//获取当前月份
YearMonth currentMonth = YearMonth.now(); //2023-06
//格式化当前月份
String currentMonth = yearMonth.format(dtf2); //2023-06
//获取当前年份
Year currentYear = Year.now(); //2023
2、Date相关操作