java 使用LocalDate根据起始时间和结束时间算出周的列表

一般我们遇到数据统计中会遇到,按照每日数据,每周,每月成交量数据展示

/**
 * 周-数据统计
 */
public class Week implements Serializable{

    /** 日期(2018-06-01~2018-06-10) **/
    private String day;
    /** 开始日期 **/
    private LocalDate startTime;
    /** 结束日期 **/
    private LocalDate endTime;
    /** 开始日期数字化 **/
    private Integer startTimeNumber;
    /** 结束日期数字化 **/
    private Integer endTimeNumber;
   //提供get和set方法
}
 
  
/**
 * 根据开始时间
 * @param startTime
 * @param endTime
 * @return
 */
public static List getWeekList(LocalDate startTime, LocalDate endTime) {
    List resultList = new ArrayList();
    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyyMMdd");

    //判断是否同一周
    WeekFields weekFields = WeekFields.of(DayOfWeek.MONDAY,4);
    if(startTime.get(weekFields.weekOfWeekBasedYear()) == endTime.get(weekFields.weekOfWeekBasedYear())){
        Week firstWeek = new Week();
        firstWeek.setDay(startTime + "~" + endTime);
        firstWeek.setStartTime(startTime);
        firstWeek.setEndTime(endTime);
        firstWeek.setStartTimeNumber(Integer.valueOf(df.format(startTime)));
        firstWeek.setEndTimeNumber(Integer.valueOf(df.format(endTime)));
        resultList.add(firstWeek);
        return resultList;
    }

    //开始周
    TemporalAdjuster FIRST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
    LocalDate startFirstWeek = startTime.with(FIRST_OF_WEEK);  //开始周开始日期
    TemporalAdjuster LAST_OF_WEEK = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
    LocalDate endFirstWeek = startTime.with(LAST_OF_WEEK);     //开始周结束日期


    //结束周
    TemporalAdjuster FIRST_OF_WEEK1 = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.minusDays(localDate.getDayOfWeek().getValue() - DayOfWeek.MONDAY.getValue()));
    LocalDate startLastWeek = endTime.with(FIRST_OF_WEEK1);
    TemporalAdjuster LAST_OF_WEEK1 = TemporalAdjusters.ofDateAdjuster(localDate -> localDate.plusDays(DayOfWeek.SUNDAY.getValue() - localDate.getDayOfWeek().getValue()));
    LocalDate endLastWeek = endTime.with(LAST_OF_WEEK1);

    //将第一周添加
    Week firstWeek = new Week();
    firstWeek.setDay(startTime + "~" + endFirstWeek);
    firstWeek.setStartTime(startTime);
    firstWeek.setEndTime(endFirstWeek);
    firstWeek.setStartTimeNumber(Integer.valueOf(df.format(startTime)));
    firstWeek.setEndTimeNumber(Integer.valueOf(df.format(endFirstWeek)));
    resultList.add(firstWeek);

    while (true) {
        startFirstWeek = startFirstWeek.plusDays(7);
        if (startFirstWeek.with(LAST_OF_WEEK).equals(startLastWeek.with(LAST_OF_WEEK1))) {
            break;
        } else {
            Week week = new Week();
            week.setDay(startFirstWeek.with(FIRST_OF_WEEK) + "~" + startFirstWeek.with(LAST_OF_WEEK));
            week.setStartTime(startFirstWeek.with(FIRST_OF_WEEK));
            week.setEndTime(startFirstWeek.with(LAST_OF_WEEK));
            week.setStartTimeNumber(Integer.valueOf(df.format(startFirstWeek.with(FIRST_OF_WEEK))));
            week.setEndTimeNumber(Integer.valueOf(df.format(startFirstWeek.with(LAST_OF_WEEK))));
            resultList.add(week);
            //System.out.println("日期="+startFirstWeek+"开始周="+startFirstWeek.with(FIRST_OF_WEEK)+"结束周="+startFirstWeek.with(LAST_OF_WEEK));
        }
    }
    Week lastWeek = new Week();
    lastWeek.setDay(startLastWeek + "~" + endTime);
    lastWeek.setStartTime(startLastWeek);
    lastWeek.setEndTime(endTime);
    lastWeek.setStartTimeNumber(Integer.valueOf(df.format(startLastWeek)));
    lastWeek.setEndTimeNumber(Integer.valueOf(df.format(endTime)));
    resultList.add(lastWeek);
    return resultList;
}
 

结果是:

日期=2018-05-02~2018-05-06开始周=2018-05-02结束周=2018-05-06开始时间戳=20180502结束时间戳=20180506
日期=2018-05-07~2018-05-13开始周=2018-05-07结束周=2018-05-13开始时间戳=20180507结束时间戳=20180513
日期=2018-05-14~2018-05-20开始周=2018-05-14结束周=2018-05-20开始时间戳=20180514结束时间戳=20180520
日期=2018-05-21~2018-05-27开始周=2018-05-21结束周=2018-05-27开始时间戳=20180521结束时间戳=20180527
日期=2018-05-28~2018-06-03开始周=2018-05-28结束周=2018-06-03开始时间戳=20180528结束时间戳=20180603
日期=2018-06-04~2018-06-10开始周=2018-06-04结束周=2018-06-10开始时间戳=20180604结束时间戳=20180610
日期=2018-06-11~2018-06-17开始周=2018-06-11结束周=2018-06-17开始时间戳=20180611结束时间戳=20180617
日期=2018-06-18~2018-06-24开始周=2018-06-18结束周=2018-06-24开始时间戳=20180618结束时间戳=20180624
日期=2018-06-25~2018-06-30开始周=2018-06-25结束周=2018-06-30开始时间戳=20180625结束时间戳=20180630

2.根据LocalDate时间查询当月第一天和最后一天

LocalDate firstDay = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
LocalDate lastDay = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
System.out.println("当月第一天="+firstDay);
System.out.println("当月最后一天="+lastDay);
结果是:
当月第一天=2018-06-01
当月最后一天=2018-06-30

你可能感兴趣的:(java)