package com.fs.utils;

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

/**
 * @description:
 * @author: Guos
 * @time: 2025/11/13 下午4:48
 */
public class MyDateUtils {

    /**
     * 生成开始时间到结束时间之间的每日时间数组
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return 时间数组列表
     */
    public static List<Date> generateDateList(Date startTime, Date endTime) {
        List<Date> dateList = new ArrayList<>();
        if (startTime == null || endTime == null) {
            return dateList;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(startTime);
        // 设置时间为当天00:00:00，确保按天计算
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        Date currentDate = calendar.getTime();
        Date finalEndTime = getStartOfDay(endTime);
        while (!currentDate.after(finalEndTime)) {
            dateList.add(currentDate);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            currentDate = calendar.getTime();
        }
        return dateList;
    }

    /**
     * 获取指定日期的开始时间(00:00:00)
     * @param date 指定日期
     * @return 当天开始时间
     */
    private static Date getStartOfDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }


}
