求两个日期的周

public List<WeekYearVO> dayOfWeekList(Integer startDate, Integer endDate){
    String startTime = String.valueOf(startDate);
    String endTime = String.valueOf(endDate);
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
    Date parse = formatter.parse(startTime);
    Date parse1 = formatter.parse(endTime);
    String startTimeDate = simpleDateFormat.format(parse);
    String endTimeDate = simpleDateFormat.format(parse1);
    DateTime startDateTime =DateTime.parse(startTimeDate);
    DateTime endDateTime = DateTime.parse(endTimeDate);

    Days days = Days.daysBetween(startDateTime, endDateTime);
    int dayCount = days.getDays();
    List<WeekYearVO> result = Lists.newArrayList();
    for (int i = 0; i <= (dayCount/7)+1; i++) {
        DateTime standardDate = startDateTime.plusWeeks(i);
        DateTime startBond = standardDate.withDayOfWeek(1);
        DateTime endBond = standardDate.withDayOfWeek(7);
        Integer week = standardDate.getWeekOfWeekyear();
        Integer year = standardDate.getWeekyear();
        WeekYearVO weekYear = new WeekYearVO(startBond.isBefore(startDateTime) ? startDateTime : startBond,
                endBond.isAfter(endDateTime) ? endDateTime : endBond,
                week,
                year);
        result.add(weekYear);
    }
    return result;
}
public class WeekYearVO {
    private DateTime startDate;
    private DateTime endDate;
    private Integer week;
    private Integer year;
}

 

posted @ 2021-11-01 17:16  江湖小学生  阅读(43)  评论(0编辑  收藏  举报