LocalDate localDate = LocalDate.of(2023,Calendar.APRIL,24);
// localDate = localDate.minusMonths(1);//月份-1
// 获取当前月的第一天
// LocalDate firstDay = localDate.with(TemporalAdjusters.firstDayOfMonth());
// System.out.println(firstDay);
// 获取当前月的最后一天
LocalDate lastDay = localDate.minusMonths(1).with(TemporalAdjusters.lastDayOfMonth());
// 第一种格式化 直接格式化
String date = lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
System.out.println(date);
// 第二种格式化 先转位Date再用SimpleDateFormat格式化
Date endTime = Date.from(lastDay.atStartOfDay(ZoneId.systemDefault()).toInstant());
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(format.format(endTime));
获取上个月第一天(包含一月和三月的时候判断上年的最后一天和二月平年和闰年)
最新推荐文章于 2024-04-30 17:28:01 发布