获取当前时间所在月份的每周日期区间(每周的起始日是周一,结束日期是周日)
例子:假设当前时间是2020-03-04,那么这个月跨度有6周。
第一周 (2020-03-01 ,2020-03-01)
第二周 (2020-03-02,2020-03-08)
第三周 (2020-03-09,2020-03-15)
第四周 (2020-03-16,2020-03-22)
第五周 (2020-03-23,2020-03-29)
第六周 (2020-03-30,2020-03-31)
代码:
public class NewDateUtil {
/**
* 获取当前时间所在月有多少周
* @param time
* @return
*/
public Integer weeksOfMonth(String time){
String timeStrs[] = time.split("-");
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, Integer.parseInt(timeStrs[0]));
c.set(Calendar.MONTH, Integer.parseInt(timeStrs[1]) - 1);
c.setFirstDayOfWeek(Calendar.MONDAY);
int weeks = c.getActualMaximum(Calendar.WEEK_OF_MONTH);
return weeks;
}
//算出所在周的周日
public String getSunOfWeek(String time) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 其实日期
LocalDate localDateate = LocalDate.parse(time, dateTimeFormatter);
LocalDate endday = localDateate.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY)).minusDays(1);
String endDayStr = endday.format(dateTimeFormatter);
return endDayStr;
}
//下一周的周一
public String getLastMonOfWeek(String time) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 其实日期
LocalDate localDateate = LocalDate.parse(time, dateTimeFormatter);
LocalDate endday = localDateate.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY));
String endDayStr = endday.format(dateTimeFormatter);
return endDayStr;
}
/**
* 获取本月周区间(每周起始和结束时间为周一到周日)
*
* @param date(yyyy-mm-dd)日期
* @return
*/
public List<WeekInfo> getScope(String date) {
int weeks = weeksOfMonth(date);
List<WeekInfo> weekInfos = new ArrayList<>();
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
// 其实日期
LocalDate localDateate = LocalDate.parse(date, dateTimeFormatter);
//月份第一周的起始时间和结束时间
LocalDate firstDay = localDateate.with(TemporalAdjusters.firstDayOfMonth());
String firstDayStr = firstDay.format(dateTimeFormatter);
String sunStr = getSunOfWeek(firstDayStr);
for (int i = 1; i <= weeks; i++) {
WeekInfo weekInfo = new WeekInfo();
//第一周的起始时间就是当月的1号,结束时间就是周日
if (i == 1) {
weekInfo.setStart(firstDayStr);
weekInfo.setEnd(sunStr);
weekInfo.setOrder(i);
//计算接下来每周的周一和周日
} else if (i < weeks) {
//由于sunStr是上一周的周日,所以取周一要取sunStr的下一周的周一
String monDay = getLastMonOfWeek(sunStr);
sunStr = getSunOfWeek(monDay);
weekInfo.setOrder(i);
weekInfo.setStart(monDay);
weekInfo.setEnd(sunStr);
//由于最后一周可能结束时间不是周日,所以要单独处理
} else {
String monDay = getLastMonOfWeek(sunStr);
//结束时间肯定就是当前月的最后一天
LocalDate lastDay = localDateate.with(TemporalAdjusters.lastDayOfMonth());
String endDay = lastDay.format(dateTimeFormatter);
weekInfo.setOrder(i);
weekInfo.setStart(monDay);
weekInfo.setEnd(endDay);
}
weekInfos.add(weekInfo);
}
return weekInfos;
}
public class WeekInfo{
private String start;
private String end;
private Integer order;
public String getStart() {
return start;
}
public void setStart(String start) {
this.start = start;
}
public String getEnd() {
return end;
}
public void setEnd(String end) {
this.end = end;
}
public Integer getOrder() {
return order;
}
public void setOrder(Integer order) {
this.order = order;
}
}
}