2020-04-20:java8 LocalDateTime提取月的每一个星期的时间段工具类

public class Java8WeekUtil { public static void main(String[] args) { //用于存放每一周的时间范围 ArrayList strings = new ArrayList<>(); //当前时间 LocalDate now = LocalDate.now(); //获取月的第一天 LocalDate startDay = now.with(TemporalAdjusters.firstDayOfMonth()); //获取月的最后一天 LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth()); //获取月的第一个星期一 LocalDate monDay = now.with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); //判断第一天是不是星期一 if (!startDay.equals(monDay)){ StringBuffer sb = new StringBuffer(); sb.append(startDay).append("-").append(monDay.plusDays(-1)); strings.add(sb.toString()); } while(monDay.isBefore(lastDay)){ LocalDate time = monDay.plusDays(6); StringBuffer sb = new StringBuffer(); if (time.isBefore(lastDay)){ sb.append(monDay).append("-").append(time); }else { sb.append(monDay).append("-").append(lastDay); } strings.add(sb.toString()); monDay= monDay.plusWeeks(1); } System.out.println(strings); }}

你可能感兴趣的:(2020-04-20:java8 LocalDateTime提取月的每一个星期的时间段工具类)