JDK8日期新特性获取七天前和上个月最后一天日期
//日期字符串格式为20210506 String coverageCharDate = "20210506"; //日期格式化变为2021-05-06(如果是20210506格式,格式化格式为yyyyMMdd) DateTimeFormatter inFormat = DateTimeFormatter.ofPattern("yyyyMMdd"); LocalDate date = LocalDate.parse(coverageCharDate, inFormat); //七天前 LocalDate weekDayDate = date.minusDays(7); //格式为字符串日期格式yyyyMMdd String weekDay = weekDayDate.format(inFormat); //上个月最后一天 LocalDate dateMonth = date.minusMonths(1); LocalDate lastMonth = dateMonth.with(TemporalAdjusters.lastDayOfMonth()); String lastMonthDate = lastMonth.format(inFormat);
获取当前时间的当月第一天和最后一天
LocalDateTime date = LocalDateTime.now();
LocalDateTime firstday = date.with(TemporalAdjusters.firstDayOfMonth());
LocalDateTime lastDay = date.with(TemporalAdjusters.lastDayOfMonth());