封装周
package com.ahies.dit.management.util;
import java.time.LocalDate;
import java.util.List;
/**
* @Description TODO
* @Date 2019/4/29 15:03
* @Author zsj
*/
public class WeekData{
// 一周的开始时间
private LocalDate start;
// 一周的结束时间
private LocalDate end;
public WeekData(List<LocalDate> localDates) {
this.start = localDates.get(0);
this.end = localDates.get(localDates.size()-1);
}
public LocalDate getStart() {
return start;
}
public void setStart(LocalDate start) {
this.start = start;
}
public LocalDate getEnd() {
return end;
}
public void setEnd(LocalDate end) {
this.end = end;
}
}
工具类方法
/**
* 给定年月获取每周的开始和结束时间
* @param year
* @param month
* @return
*/
public static Map<Integer,WeekData> weeks(int year,int month){
LocalDate start = LocalDate.now().withYear(year).withMonth(month).with(TemporalAdjusters.firstDayOfMonth());
LocalDate end = LocalDate.now().withYear(year).withMonth(month).with(TemporalAdjusters.lastDayOfMonth());
Map<Integer, WeekData> map = Stream.iterate(start, localDate -> localDate.plusDays(1))
.limit(ChronoUnit.DAYS.between(start, end)+1)
.collect(Collectors.groupingBy(localDate -> localDate.get(WeekFields.of(DayOfWeek.MONDAY, 1).weekOfMonth()),
Collectors.collectingAndThen(Collectors.toList(), WeekData::new)));
return map;
}