package com.docus.server.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DateSpiltUtil {



    /**
     *切割起始时间和结束时间，按照一天一天切割。不足一天则不切割
     * @param startDate  起始时间
     * @param endDate   结束时间
     * @param periodTime  切割时间段，毫秒
     * @return
     */
    public static List<PeriodTime> spiltDate(Date startDate, Date endDate, long periodTime) {

        List<PeriodTime> periodTimeList = new ArrayList<>();

        boolean isEnd = false;

        Date periodStartDate = startDate;
        do {
            Date periodEndDate = new Date(periodStartDate.getTime() + periodTime);
            if (endDate.before(periodEndDate) || endDate.compareTo(periodStartDate) == 0) {
                periodEndDate = endDate;
                isEnd = true;
            }

            //加入时段中
            periodTimeList.add(new PeriodTime(periodStartDate, periodEndDate));
            periodStartDate = periodEndDate;

        } while (!isEnd);

        return periodTimeList;

    }
}
