package com.ruoyi.app.utils;

import lombok.SneakyThrows;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

    //获取当前时间戳
    public Long getTimeMillis() throws ParseException {
        return System.currentTimeMillis();
    }
    //判断多少天后的日期
    public String addDate(String date,int days) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = format.parse(date);
        calendar.setTime(parse);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        // days  多少天后的日期
        int newDay = day+days;
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,month);
        calendar.set(Calendar.DAY_OF_MONTH,newDay);
        return format.format(calendar.getTime());
    }
    //时间字符串转为时间
    @SneakyThrows
    public Date getDatetim(String dateString) {

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = dateFormat.parse(dateString);
        return date;
    }
    //转换时间为字符串
    public String setDate(Date date) {

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(date);
    }

    //获取当前日期
    public String GetDate() {
        Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(calendar.getTime());
    }
    //获取当前日期(不带时间)
    public String GetDateNt() {
        Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(calendar.getTime());
    }
    //获取当前日期(无格式)
    public String GetDatewjg() {
        Calendar calendar = Calendar.getInstance(); // get current instance of the calendar
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return formatter.format(calendar.getTime());
    }
    @SneakyThrows
    public  boolean isLegalTime(String startTime, String endTime){
        Date currentDate = new Date();
        String format = "HH:mm";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String nowtTime = sdf.format(currentDate);
        Date nowDate = sdf.parse(nowtTime);
        Date startDate = sdf.parse(startTime);
        Date endDate = sdf.parse(endTime);

        if (nowDate.getTime() == startDate.getTime()
                || nowDate.getTime() == endDate.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowDate);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startDate);

        Calendar end = Calendar.getInstance();
        end.setTime(endDate);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    //判断日期是否小于当前日期
    public boolean isPastDate(String str){

        boolean flag = false;
        Date nowDate = new Date();
        Date pastDate = null;
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.CHINA);
        //在日期字符串非空时执行
        if (str != null && !"".equals(str)) {
            try {
                //将字符串转为日期格式，如果此处字符串为非合法日期就会抛出异常。
                pastDate = sdf.parse(str);
                //调用Date里面的before方法来做判断
                flag = pastDate.before(nowDate);
                if (flag) {
                    System.out.println("该日期早于今日");
                }else {
                    System.out.println("该日期晚于今日");
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else {
            System.out.println("日期参数不可为空");
        }
        return flag;
    }

    /**
     * 获取指定秒数之后的时间,返回小时和分钟
     * @param seconds
     * @return
     */
    public String getHourAndMinute(long seconds) {
        LocalTime now = LocalTime.now(); // 当前时间
        LocalTime newTime = now.plusSeconds(seconds); // 加上秒数
        String result = newTime.format(DateTimeFormatter.ofPattern("HH:mm"));
        return result;
    }
}
