package shop.alien.util.date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期计算工具类
 *
 * @author ssk
 * @version 1.0
 * @date 2023/5/24 16:03
 */
public class DateUtils {

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date date = calcHours(new Date(), 1);
//        System.out.println(sdf.format(date));
//
//        boolean b = dateCompare(new Date(), date);
//        System.out.println(b);


        Date date1 = sdf.parse("2024-05-10 19:10:00");
        Date date2 = sdf.parse("2024-05-10 19:11:00");
        System.out.println(calcMinute(date1, 1));
        System.out.println(dateCompare(date1, calcMinute(date1, 1)));

    }

    /**
     * 天数加减
     *
     * @param date 日期
     * @param days 加减天数
     * @return 处理后的日期
     */
    public static Date calcDays(Date date, Integer days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, days);
        return c.getTime();
    }

    /**
     * 小时加减
     *
     * @param date  日期
     * @param hours 加减时间
     * @return 处理后的日期
     */
    public static Date calcHours(Date date, Integer hours) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, hours);
        return c.getTime();
    }

    /**
     * 分钟加减
     *
     * @param date   日期
     * @param minute 加减时间
     * @return 处理后的日期
     */
    public static Date calcMinute(Date date, Integer minute) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, minute);
        return c.getTime();
    }

    /**
     * 日期比较
     *
     * @param dateOne 日期1
     * @param dateTwo 日期2
     * @return 是否大于
     */
    public static boolean dateCompare(Date dateOne, Date dateTwo) {
        return dateOne.compareTo(dateTwo) > 0;
    }

    /**
     * 今日剩余秒数
     *
     * @return int
     */
    public static Integer todayLastSecond() {
        int daySecond = 86400;
        LocalDateTime now = LocalDateTime.now();
        int hour = now.getHour() * 60 * 60;
        int minute = now.getMinute() * 60;
        int second = now.getSecond();
        return daySecond - hour - minute - second;
    }
}
