//日期字符串格式為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());