package com.kingdee.eas.custom.certification.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

/**
 * 日期工具类
 * @author coyle
 * @date 2024-11-21
 */
public class DateUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);

    public static final String DATE_FORMAT_YYYY_MM_DD  = "yyyy-MM-dd";
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";
    private static SimpleDateFormat SDFUTC = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
    private DateUtil() {
        throw new IllegalStateException("Utility class");
    }

    private static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 截取去掉月份或天多余的0
     * @param date
     * @return
     */
    public static String simplify(String date){
        int index1 =date.indexOf("-");
        int index2 =date.lastIndexOf("-");
        int month;
        int day;
        int year=Integer.parseInt(date.substring(0,index1));
        if(date.substring(index1+1,index1+2)=="0"){
            month=Integer.parseInt(date.substring(index1+2,index1+3));
        }else {
            month=Integer.parseInt(date.substring(index1+1,index1+3));
        }
        if(date.substring(index2+1,index2+2)=="0"){
            day=Integer.parseInt(date.substring(index2+2,index2+3));
        }else{
            day=Integer.parseInt(date.substring(index2+1,index2+3));
        }
        String date1=(month+"."+day)+"";
        return  date1;
    }

    /**
     *  根据年份和周数 获取周一和周日日期
     * @return
     */
    public static List<Map> getDataScope(String date){
        String[] timeStrs = date.split("-");
        Calendar c = Calendar.getInstance();
        c.set(Calendar.YEAR, Integer.parseInt(timeStrs[0]));
        c.set(Calendar.MONTH, Integer.parseInt(timeStrs[1]) - 1);
        c.setFirstDayOfWeek(Calendar.MONDAY);
        int weeks = c.getActualMaximum(Calendar.WEEK_OF_MONTH);

        LocalDate localDateate = LocalDate.parse(date, dateTimeFormatter);
        //月份第一周的起始时间和结束时间
        LocalDate firstDay = localDateate.with(TemporalAdjusters.firstDayOfMonth());
        String firstDayStr = firstDay.format(dateTimeFormatter);
        String sunStr = getSunOfWeek(firstDayStr);

        List<Map> weekInfos = new ArrayList<>();
        for (int i = 1; i <= weeks; i++) {
            Map weekInfo = new HashMap();
            //第一周的起始时间就是当月的1号，结束时间就是周日
            if (i == 1) {
                weekInfo.put("start",firstDayStr);
                weekInfo.put("end",sunStr);
                weekInfo.put("order",i);
                //计算接下来每周的周一和周日
            } else if (i < weeks) {
                //由于sunStr是上一周的周日，所以取周一要取sunStr的下一周的周一
                String monDay = getLastMonOfWeek(sunStr);
                sunStr = getSunOfWeek(monDay);
                weekInfo.put("start",monDay);
                weekInfo.put("end",sunStr);
                weekInfo.put("order",i);
                //由于最后一周可能结束时间不是周日，所以要单独处理
            } else {
                String monDay = getLastMonOfWeek(sunStr);
                //结束时间肯定就是当前月的最后一天
                LocalDate lastDay = localDateate.with(TemporalAdjusters.lastDayOfMonth());
                String endDay = lastDay.format(dateTimeFormatter);
                weekInfo.put("start",monDay);
                weekInfo.put("end",endDay);
                weekInfo.put("order",i);
            }

            weekInfos.add(weekInfo);

        }
        return weekInfos;
    }

    public static boolean isEffectiveDate2(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

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

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

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

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


    //算出所在周的周日
    public static String getSunOfWeek(String time) {
        LocalDate localDateate = LocalDate.parse(time, dateTimeFormatter);
        LocalDate endday = localDateate.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY)).minusDays(1);
        String endDayStr = endday.format(dateTimeFormatter);
        return endDayStr;
    }

    //下一周的周一
    public static String getLastMonOfWeek(String time) {
        LocalDate localDateate = LocalDate.parse(time, dateTimeFormatter);
        LocalDate endday = localDateate.with(TemporalAdjusters.next(java.time.DayOfWeek.MONDAY));
        String endDayStr = endday.format(dateTimeFormatter);
        return endDayStr;
    }


    /**
     * 判断是上午 还是 下午
     * @param date
     */
    public static  String ifAfternoonByDate(Date date) {
        //结果为“0”是上午 结果为“1”是下午
        Calendar calendar =  Calendar.getInstance();
        calendar.setTime(date);
        GregorianCalendar gregorianCalendar = (GregorianCalendar)calendar;
        int index = gregorianCalendar.get(GregorianCalendar.AM_PM);
        if (index == 0) {
            return "上午";
        }
        return "下午";
    }
    /**
     * 日期转字符串
     * @param date   日期
     * @param format 格式化
     * @return 日期字符串
     */
    public static String dateToString(Date date, String format) {
        return new SimpleDateFormat(format).format(date);
    }
    public static String dateToUtc(Date date) {
        SDFUTC.setTimeZone(TimeZone.getTimeZone("UTC"));
//        sdfutc.setTimeZone(TimeZone.getTimeZone("GMT"));
        return SDFUTC.format(date);
    }
    /**
     * 字符串转日期
     * @param dateStr 日期字符串
     * @param format  格式化
     * @return 日期
     */
    public static Date stringToDate(String dateStr, String format) {
        try {
            return new SimpleDateFormat(format).parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
            LOGGER.error(String.format("无法将字符串%s转换为日期[%s]", dateStr, format), e);
        }
        return null;
    }
    /**
     * 字符串转日期
     * @param dateStr 日期字符串
     * @param format  格式化
     * @return 日期
     */
    public static Date stringToDate2(String dateStr, String format) {
        try {
            return new SimpleDateFormat(format).parse(dateStr);
        } catch (ParseException e) {
            LOGGER.error(String.format("无法将字符串%s转换为日期[%s]", dateStr, format), e);
        }
        return new Date();
    }

    /**
     * 一个月所有月份
     * @param month
     * @author lsw
     * @return
     */
    public static List<String> dayReport(Date month) {
        List<String> fullDayList = new ArrayList<String>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(month);//month 为指定月份任意日期
        int year = cal.get(Calendar.YEAR);
        int m = cal.get(Calendar.MONTH);
        int dayNumOfMonth = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, 1);// 从一号开始

        for (int i = 0; i < dayNumOfMonth; i++, cal.add(Calendar.DATE, 1)) {
            Date d = cal.getTime();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String df = simpleDateFormat.format(d);
            fullDayList.add(df);
        }
        return fullDayList;
    }
    /**
     * 获取两个日期之间的所有日期集合
     * @param start 开始日期
     * @param end   结束日期
     * @return 所有日期集合
     */
    public static List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(getDayBegin(start));

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(getDayBegin(end));
        do {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        } while (!tempStart.after(tempEnd));
        return result;
    }
    /**
     * 获取两个日期之间的所有月份集合
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return List<String>

     */
    public static List<String> getBetweenMonths(Date startDate, Date endDate) {
        startDate = startDate==null? new Date(): startDate;
        endDate = endDate==null? new Date(): endDate;

        Calendar start = Calendar.getInstance();
        start.setTime(startDate);//设置起始时间
        start.set(Calendar.DATE, 1);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);//设置结束时间
        end.set(Calendar.DATE, 2);

        List<String> months = new ArrayList<>();
        do {
            months.add(DateUtil.dateToString(start.getTime(), "yyyy_MM"));
            start.add(Calendar.MONTH, 1);
        } while (start.before(end));
        return months;
    }
    /**
     * 获取两个日期之间的每月最后一天
     * @param startDate 开始日期
     * @param endDate   结束日期
     * @return List<String>
     */
    public static List<Date> getBetweenLastMonths(Date startDate, Date endDate) {
        startDate = startDate==null? new Date(): startDate;
        endDate = endDate==null? new Date(): endDate;

        Calendar start = Calendar.getInstance();
        start.setTime(startDate);//设置起始时间
        start.set(Calendar.DATE, 1);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);//设置结束时间
        end.set(Calendar.DATE, 2);

        List<Date> months = new ArrayList<>();
        do {
            months.add(DateUtil.lastDayOfMonth(start.getTime()));
            start.add(Calendar.MONTH, 1);
        } while (start.before(end));
        return months;
    }
    /**
     * 获取日期范围内的所有间隔日期
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param interval 间隔天数
     * @param includeEnd 是否间隔结束日期是否在结束日期内
     * @return List<Date>
     */
    public static List<Date> getIntervalDays(Date startDate, Date endDate, int interval, boolean includeEnd) {
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);//设置结束时间
        end.add(Calendar.DAY_OF_YEAR, 1);

        Calendar start = Calendar.getInstance();
        start.setTime(startDate);//设置起始时间
        start.add(Calendar.DAY_OF_YEAR, interval);
        if (!start.before(end)) {
            return new ArrayList<>();
        }
        List<Date> dateList = new ArrayList<>();
        do {
            dateList.add(start.getTime());
            start.add(Calendar.DAY_OF_YEAR, interval);
        } while (start.before(end));
        if (includeEnd && start.after(end)) {
            dateList.remove(dateList.size() - 1);
        }
        return dateList;
    }
    /**
     * 日期计算
     * @param date 日期
     * @param num 增加天数
     * @return Date
     */
    public static Date add(Date date, int num) {
        if (date == null) {
            return null;
        }
        return DateUtil.add(date, num, Calendar.DAY_OF_YEAR);
    }
    /**
     * 日期计算
     * @param date 日期
     * @param num 增加数
     * @param type 增加类型
     * @return Date
     */
    public static Date add(Date date, int num, int type) {
        Calendar start = Calendar.getInstance();
        start.setTime(date);//设置起始时间
        start.add(type, num);
        return start.getTime();
    }
    /**
     * 日期计算
     * @param date 日期
     * @param num 增加数
     * @param type 增加类型
     * @return Date
     */
    public static Calendar calAdd(Date date, int num, int type) {
        Calendar start = Calendar.getInstance();
        start.setTime(date);//设置起始时间
        start.add(type, num);
        return start;
    }
    /**
     * 获取一天最后一秒
     * @param date
     * @return
     */
    public static Date getLastSecond(Date date){
        date = getDayBegin(date);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DAY_OF_MONTH,1);
        cal.add(Calendar.SECOND,-1);
        return cal.getTime();
    }
    /**
     * 日期所在月份最后一天
     * @param date 日期
     * @return 月份最后一天
     * @author 李帅伟
     * @since 2021年05月10日-10:55
     */
    public static Date lastDayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * //获取当前月第一天：
     * @param date
     * @return
     */
    public static Date firstDayOfMonth(Date date)  {
        //获取当前月第一天：
        Calendar   cal=Calendar.getInstance();//获取当前日期
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH,1);
        return cal.getTime();
    }
    /**
     * //获取当前月第一天：
     * @param time
     * @return
     */
    public static String firstDay(String time) throws ParseException {

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(time.replace("_","-")));
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH,1);
        return format.format(calendar.getTime());
    }

    /**
     * //获取当前月最后一天
     * @param time
     * @return
     */
    public static String lastDay(String time) throws ParseException {

        Calendar cal = Calendar.getInstance();
        cal.setTime(sdf.parse(time.replace("_","-")));
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(cal.getTime());
    }

    public static int getDaysOfMonth(String time) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(sdf.parse(time.replace("_","-")));
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }
    public static Calendar toCalendar(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal;
    }
    /**a
     * 间隔天数
     * @param cal1
     * @param cal2
     * @return
     */
    public static int getBetweendaynum(Calendar cal1, Calendar cal2){
        cal1.set(cal1.get(Calendar.YEAR), cal1.get(Calendar.MONTH), cal1.get(Calendar.DAY_OF_MONTH));
        cal2.set(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH), cal2.get(Calendar.DAY_OF_MONTH));
        long time1 = cal1.getTimeInMillis();
        long time2 = cal2.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return (int) between_days;
    }
    public static int getDifDay(Date start, Date end) {
        if (start == null || end == null) {
            return 0;
        }
        return getBetweendaynum(toCalendar(getDayBegin(start)), toCalendar(getDayBegin(end)));
    }
    public static int getDifDay(Date start, Date end, int defaultDiff) {
        if (start == null || end == null) {
            return defaultDiff;
        }
        return getBetweendaynum(toCalendar(getDayBegin(start)), toCalendar(getDayBegin(end)));
    }
    /**
     * 间隔月数
     * @param start
     * @param end
     * @return
     */
    public static Integer getDifMonth(Calendar start, Calendar end){
        int result = end.get(Calendar.MONTH) - start.get(Calendar.MONTH);
        int month = (end.get(Calendar.YEAR) - start.get(Calendar.YEAR)) * 12;
        return month + result;
    }
    public static Integer getDifMonth(Date start, Date end){
        Calendar startCal = Calendar.getInstance();
        startCal.setTime(start);
        Calendar endCal = Calendar.getInstance();
        endCal.setTime(end);
        int result = endCal.get(Calendar.MONTH) - startCal.get(Calendar.MONTH);
        int month = (endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR)) * 12;
        return month + result;
    }
    /**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

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

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

        return date.after(begin) && date.before(end);
    }

    /**
     * 判断是否同一年
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDate(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR);
    }
    /**
     * 判断是否同一年
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameYearMonth(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2
                .get(Calendar.MONTH);
    }

    public static boolean isSameYearMonthDay(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return cal1.get(Calendar.YEAR) == cal2
                .get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2
                .get(Calendar.MONTH)&& cal1.get(Calendar.DAY_OF_MONTH) == cal2
                .get(Calendar.DAY_OF_MONTH);
    }
    /**
     * 获取平均日期
     */
    public static Date getAvgDate(List<Date> dateList) {
        if (dateList.isEmpty()) {
            return null;
        }
        int size = 0;
        long time = 0;
        for (Date date : dateList) {
            if (date != null) {
                time += date.getTime();
                size ++;
            }
        }
        if (time == 0 || size == 0) {
            return null;
        }
        return new Date(time / size);
    }

    /**
     * 判断是否同一月
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameMouth(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return cal1.get(Calendar.MONTH) == cal2
                .get(Calendar.MONTH);
    }
    /**
     * 清空时分秒
     */
    public static Date getDayBegin(Date date) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    /**
     * 获取一天最后一秒
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }

    public static Date getYearStart(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int year = DateUtil.toCalendar(date).get(Calendar.YEAR);
        try {
            return format.parse(year + "-01-01 00:00:00");
        } catch (ParseException e) {
            LOGGER.error("日期转换错误", e);
        }
        return null;
    }

    public static Date getYearFirstDay(Date date){
        int year = DateUtil.toCalendar(date).get(Calendar.YEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        return calendar.getTime();
    }

    public static Date getYearLastDay(Date date) {
        int year = DateUtil.toCalendar(date).get(Calendar.YEAR);
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        return calendar.getTime();
    }

    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDateStartTime(date));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    public static Date getNextMonthFirstDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(getDateStartTime(date));
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 将指定日期的时分秒格式那天的 00:00:00 的时间
     * @param date 指定日期
     * @return date
     */
    public static Date getDateStartTime(Date date) {
        ZoneId utcPlus8 = ZoneId.systemDefault();
        LocalDate localDate = date.toInstant().atZone(utcPlus8).toLocalDate();
        LocalDateTime localDateTime = localDate.atStartOfDay();
        return Date.from(localDateTime.atZone(utcPlus8).toInstant());
    }
    /**
     * 将指定日期的时分秒格式那天的 23:59:59 的时间
     * @param date 指定日期
     * @return date
     */
    public static Date getDateEndTime(Date date) {
        ZoneId utcPlus8 = ZoneId.systemDefault();
        LocalDate localDate = date.toInstant().atZone(utcPlus8).toLocalDate();
        LocalDateTime localDateTime = localDate.atTime(23, 59, 59);
        return Date.from(localDateTime.atZone(utcPlus8).toInstant());
    }

    /**
     * 获取俩个日期间天数，如果入参为空时返回0
     * @param firstDate 开始时间
     * @param secDate 结束时间
     * @return 返回俩个日期间天数的绝对值
     */
    public static int daysBetweenIfBlankReturnZero(Date firstDate, Date secDate) {
        if (firstDate == null || secDate == null) {
            return 0;
        }
        return daysBetween(firstDate, secDate);
    }

    /**
     * 获取俩个日期间天数 ( 正整数 )
     * @param firstDate 开始时间
     * @param secDate 结束时间
     * @return 返回俩个日期间天数的绝对值
     */
    public static int daysBetween(Date firstDate, Date secDate) {
        ZoneId utcPlus8 = ZoneId.systemDefault();
        LocalDate localDate1 = firstDate.toInstant().atZone(utcPlus8).toLocalDate();
        LocalDate localDate2 = secDate.toInstant().atZone(utcPlus8).toLocalDate();
        return localDate2.isAfter(localDate1) ? (int)localDate1.until(localDate2, ChronoUnit.DAYS) : (int)localDate2.until(localDate1, ChronoUnit.DAYS);
    }

    /**
     * 获取俩个日期间天数 (前面日期小,为负数)
     * a-b = ? 的效果
     * @param firstDate 开始时间
     * @param secDate 结束时间
     * @return 返回俩个日期间天数的差额
     * @author 匡原志
     */
    public static int daysDifference(Date firstDate, Date secDate) {
        ZoneId utcPlus8 = ZoneId.systemDefault();
        LocalDate localDate1 = firstDate.toInstant().atZone(utcPlus8).toLocalDate();
        LocalDate localDate2 = secDate.toInstant().atZone(utcPlus8).toLocalDate();
        return   (int)localDate2.until(localDate1, ChronoUnit.DAYS);
    }


    /**
     * 获取离当前最近的 i 点时间
     * @param i
     * @return
     */
    public static Date getNearDate(Integer i) {
        // 获取当前系统时间
        LocalDateTime toDay = LocalDateTime.now();
        /*
          判断当天时间是否小于 i 点
          true: 获取当天早上8点
          false: 获取明天早上8点时间
         */
        LocalDateTime dateTime = null;
        if(toDay.getHour() < i){
            dateTime = LocalDateTime.of(toDay.toLocalDate(), LocalTime.MIN.withHour(i));
        } else {
            // 获取当天早上8点时间  时间精确到i:00:00
            dateTime = LocalDateTime.of(toDay.toLocalDate(), LocalTime.MIN.withHour(i));
            // 根据当前时间获取第二天早上i点时间精确到i:00:00
            dateTime  = dateTime.plusDays(1);
        }
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        // 本地时间线LocalDateTime到即时时间线Instant时间戳
        Instant instant = zonedDateTime.toInstant();
        // UTC时间(世界协调时间,UTC + 00:00)转北京(北京,UTC + 8:00)时间
        return Date.from(instant);
    }


    /**
     * 得到前一天的日期
     * @return
     */
    public static Date getYesterday() {
        Calendar calendar = Calendar.getInstance();
        //得到前一天
        calendar.add(Calendar.DATE, -1);
        return calendar.getTime();
    }



    /**
     * 将字符串转日期成Long类型的时间戳，格式为：yyyy-MM-dd HH:mm:ss
     */
    public static Long convertTimeToLong(String time) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            Date newTime= format.parse(time);
            return newTime.getTime();
        } catch (ParseException e) {
            LOGGER.error(String.format("无法将字符串%s转换为日期[%s]", time, format), e);
        }
        return null;
    }


}
