Java 获取除当前周外 ,指定几周前的开始时间
/**
* JCccc
* @param num
* @return
*/
private static String getRecent5WeeksStartEnd(Integer num) {
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDateTime nowWeekStart = LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)).atStartOfDay();
LocalDateTime dayBeforeDate = nowWeekStart.minusDays(num*7);
return dayBeforeDate.format(pattern);
}
比如今天是周日, 获取前1周的开始时间,也就是 15号才对 ,如果是前2周 就是 8号:
public static void main(String[] args) throws Exception {
String date1 = getRecent5WeeksStartEnd(1);
System.out.println(date1);
String date2 = getRecent5WeeksStartEnd(2);
System.out.println(date2);
}
效果:
还没有评论,来说两句吧...