package com.ho.xxljob.util;

import cn.hutool.core.date.DateUtil;

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

/**
 * @author fancl
 * @desc: 时间转换
 * @date 2022/10/19
 */

public class TimeConvertUtil {

    //时间字符串格式(以分钟为单位)
    public static List<String> minute(Date date, int offset) {
        Date start = dayStartDate(date);//转换为天的起始date
        Date nextDayDate = nextDay(start);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        List<String> result = new ArrayList<>(720);
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //日期加5分钟
            start = addMinutes(start, offset);
        }

        return result;
    }

    public static List<String> minute2(Date date, int offset) {
        Date start = dayStartDate(date);//转换为天的起始date
        Date nextDayDate = nextDay(start);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<String> result = new ArrayList<>(20);
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //日期加5分钟
            start = addMinutes(start, offset);
        }

        return result;
    }

    //以天为单位
    public static List<String> day(Date date, int offset) {
        Date start = DateUtil.beginOfMonth(date);//转换为天的起始date
        Date nextDayDate = DateUtil.endOfMonth(date);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> result = new ArrayList<>(getCurrentMonthLastDay());
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //日期加一天
            start = addDays(start, offset);
        }
        return result;
    }

    //以月为单位
    public static List<String> month(Date date, int offset) {
        Date start = DateUtil.beginOfYear(date);//转换为天的起始date
        Date nextDayDate = DateUtil.endOfYear(date);//下一天的date
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<String> result = new ArrayList<>(20);
        while (start.compareTo(nextDayDate) < 0) {
            result.add(sdf.format(start));
            //加一个月
            start = addMonth(start, offset);
        }
        return result;
    }

    //增加时间间隔
    public static Date addMinutes(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.MINUTE, offset);
        return c.getTime();
    }
    public static Date addDays(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.DAY_OF_MONTH, offset);
        return c.getTime();
    }

    private static Date addMonth(Date start, int offset) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.MONTH, offset);
        return c.getTime();
    }

    public static Date nextDay(Date start) {
        Calendar c = Calendar.getInstance();
        c.setTime(start);
        c.add(Calendar.DATE, 1);
        return c.getTime();
    }

    public static Date dayStartDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

    public static int getCurrentMonthLastDay()
    {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);//把日期设置为当月第一天
        a.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
}
