1.第一种情况,从1号开始到月份最后一天结束
代码如下:
private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");public List<Map> getScope() {String date = "-06-01";String timeStrs[] = date.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);LocalDate localDateate = LocalDate.parse(date, dateTimeFormatter);//月份第一周的起始时间和结束时间LocalDate firstDay = localDateate.with(TemporalAdjusters.firstDayOfMonth());String firstDayStr = firstDay.format(dateTimeFormatter);String sunStr = getSunOfWeek(firstDayStr);List<Map> weekInfos = new ArrayList<>();for (int i = 1; i <= weeks; i++) {Map weekInfo = new HashMap();//第一周的起始时间就是当月的1号,结束时间就是周日if (i == 1) {weekInfo.put("start",firstDayStr);weekInfo.put("end",sunStr);weekInfo.put("order",i);//计算接下来每周的周一和周日} else if (i < weeks) {//由于sunStr是上一周的周日,所以取周一要取sunStr的下一周的周一String monDay = getLastMonOfWeek(sunStr);sunStr = getSunOfWeek(monDay);weekInfo.put("start",monDay);weekInfo.put("end",sunStr);weekInfo.put("order",i);//由于最后一周可能结束时间不是周日,所以要单独处理} else {String monDay = getLastMonOfWeek(sunStr);//结束时间肯定就是当前月的最后一天LocalDate lastDay = localDateate.with(TemporalAdjusters.lastDayOfMonth());String endDay = lastDay.format(dateTimeFormatter);weekInfo.put("start",monDay);weekInfo.put("end",endDay);weekInfo.put("order",i);}weekInfos.add(weekInfo);}return weekInfos;}//算出所在周的周日public String getSunOfWeek(String time) {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) {LocalDate localDateate = LocalDate.parse(time, dateTimeFormatter);LocalDate endday = localDateate.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY));String endDayStr = endday.format(dateTimeFormatter);return endDayStr;}
运行结果:
2.第二种情况,每周固定从周一开始到周天结束
代码如下:
private DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");public List<Map> getScope() {String date = "-06-01";String timeStrs[] = date.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);LocalDate localDateate = LocalDate.parse(date, dateTimeFormatter);//月份第一周的起始时间和结束时间LocalDate firstDay = localDateate.with(java.time.DayOfWeek.MONDAY);String firstDayStr = firstDay.format(dateTimeFormatter);String sunStr = getSunOfWeek(firstDayStr);List<Map> weekInfos = new ArrayList<>();for (int i = 1; i <= weeks; i++) {Map weekInfo = new HashMap();//第一周的起始时间就是当月的1号,结束时间就是周日if (i == 1) {weekInfo.put("start",firstDayStr);weekInfo.put("end",sunStr);weekInfo.put("order",i);//计算接下来每周的周一和周日} else {//由于sunStr是上一周的周日,所以取周一要取sunStr的下一周的周一String monDay = getLastMonOfWeek(sunStr);sunStr = getSunOfWeek(monDay);weekInfo.put("start",monDay);weekInfo.put("end",sunStr);weekInfo.put("order",i);}weekInfos.add(weekInfo);}return weekInfos;}//算出所在周的周日public String getSunOfWeek(String time) {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) {LocalDate localDateate = LocalDate.parse(time, dateTimeFormatter);LocalDate endday = localDateate.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY));String endDayStr = endday.format(dateTimeFormatter);return endDayStr;}
运行结果: