package com.weiqi.mis;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.util.StringUtils;

/**
 * Created by yinqingzhun on 2024/6/1.
 */
@Slf4j
public class DateHelper {

    public final static String DATEFORMAT_FULL = "yyyy-MM-dd HH:mm:ss.SSS";
    public final static String DATEFORMAT_STANDARD = "yyyy-MM-dd HH:mm:ss";
    public final static String DATEFORMAT_STANDARD_HH_MM = "yyyy-MM-dd HH:mm";
    public final static String DATEFORMAT_ONLY_MONTH = "yyyy-MM";

    public final static String DATEFORMAT_ONLY_DATE = "yyyy-MM-dd";
    public final static String DATEFORMAT_ONLY_DATE_SHORT = "yyyyMMdd";
    public final static String DATEFORMAT_NEW = "yyyy/MM/dd HH:mm:ss";
    public final static String DATEFORMAT_FULL_PURE_NUMBER = "yyyyMMddHHmmssSSS";
    public final static String DATEFORMAT_FULL_PURE_NUMBER_SHORT = "yyyyMMddHHmmss";
    public final static String FULL_TIME_PATTERN = "HH:mm:ss.SSS";
    public final static String FULL_TIME_PATTERN_SHORT = "HH:mm:ss";
    public final static String FULL_TIME_PATTERN_SHORT_T = "HHmmss";
    public final static String DATEFORMONTH_PATTERN_SHORT = "MM-dd HH:mm";
    public final static String DATEFORMONTH_PATTERN_SHORT2 = "MM-dd HH:mm:ss";
    /**
     * "2019-01-30T13:16:06.950+0000"
     * "2019-01-30T13:16:06.950"
     * "2019-01-30T13:16:06"
     */
    public final static String DATEFORMAT_SPECIAL_T = "yyyy-MM-dd'T'HH:mm:ss";
    public final static LocalDateTime UNIX_START_LOCAL_DATE_TIME = LocalDateTime.of(1970, 1, 1, 0, 0, 0);


    public static long getDiffDays(Date date1, Date date2) {
        LocalDateTime l1 = date1.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        LocalDateTime l2 = date2.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        if (l1.isAfter(l2)) {
            LocalDateTime temp;
            temp = l2;
            l2 = l1;
            l1 = temp;
        }
        Duration between = Duration.between(l1, l2);
        return between.toDays();
    }

    public static long getDiffDays(LocalDate date1, LocalDate date2) {
        Duration between = Duration.between(date1, date2);
        return between.toDays();
    }

    //    public static void main(String[] args) {
//        System.out.println(getDiffDays(deserialize("2022-10-01","yyyy-MM-dd"),deserialize("2022-10-02",
//        "yyyy-MM-dd")));
//    }
    public static Date getLastDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String dayTime = serialize(cal.getTime(), DATEFORMAT_ONLY_DATE) + " 00:00:00";
        return deserialize(dayTime, DATEFORMAT_STANDARD);
    }


    public static Date deserialize(String source, String... dateFormat) {
        Date date = null;
        if (dateFormat != null && dateFormat.length > 0) {
            for (String df : dateFormat) {
                try {
                    date = new SimpleDateFormat(df).parse(source);
                    break;
                } catch (Exception e) {
                    log.debug(e.getMessage());
                }
            }

        } else {
            // sample: /Date(1528805593000)/
            Matcher matcher = Pattern.compile("\\/Date\\((-?\\d+)\\)\\/").matcher(source);
            if (matcher.matches()) {
                date = new Date(Long.valueOf(matcher.group(1)));
            } else {
                try {
                    date = parse(source);
                } catch (ParseException e) {
                    log.debug(e.getMessage());
                }
            }


        }
        Preconditions.checkNotNull(date, String.format("date %s can't be resolved", source));
        return date;

    }

    /**
     * 使用dateFormat格式化Date对象
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String serialize(Date date, String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        String str = df.format(date);
        return str;
    }

    /**
     * 使用dateFormat格式化LocalDateTime对象
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String serialize(LocalDateTime date, String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        String str = df.format(localDateTimeToDate(date));
        return str;
    }

    /**
     * 使用dateFormat格式化LocalDate对象
     *
     * @param date
     * @param dateFormat
     * @return
     */
    public static String serialize(LocalDate date, String dateFormat) {
        DateFormat df = new SimpleDateFormat(dateFormat);
        String str = df.format(localDateToDate(date));
        return str;
    }

    /**
     * 使用yyyy-MM-dd HH:mm:ss.SSS格式化date对象
     *
     * @param date
     * @return
     */
    public static String serialize(Date date) {
        DateFormat df = new SimpleDateFormat(DateHelper.DATEFORMAT_FULL);
        String str = df.format(date);
        return str;
    }

    /**
     * 返回代表当前时间格式化的字符串（格式：yyyy-MM-dd HH:mm:ss.SSS）
     *
     * @return
     */
    public static String getNowString() {
        return serialize(getNow(), DATEFORMAT_FULL);
    }

    /**
     * 返回代表当前日期格式化的字符串的数字（格式：yyyyMMdd）
     *
     * @return
     */
    public static int getNowOnlyDateShortString() {
        return Integer.parseInt(serialize(getNow(), DATEFORMAT_ONLY_DATE_SHORT));
    }

    /**
     * 返回代表当前时间格式化的字符串
     *
     * @return
     */
    public static String getNowString(String dateFormat) {
        return serialize(getNow(), dateFormat);
    }

    /**
     * 返回一个代表当前时间的Date对象
     *
     * @return
     */
    public static Date getNow() {
        return Calendar.getInstance().getTime();
    }

    /**
     * 返回代表当前时间的毫秒数
     *
     * @return
     */
    public static long getNowInInMillis() {
        return Calendar.getInstance().getTimeInMillis();
    }

    /**
     * 获取当前日期加上指定时间长度以后的Date
     *
     * @param timeUnit 时间单位
     * @param account  添加到calendarTimeField的时间或日期数量
     * @return
     */
    public static Date getDate(TimeUnit timeUnit, int account) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(timeUnit.getValue(), account);
        return calendar.getTime();
    }

    /**
     * 加减指定单位的时间
     *
     * @param date
     * @param timeUnit
     * @param account
     * @return
     */
    public static Date add(Date date, TimeUnit timeUnit, int account) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(timeUnit.getValue(), account);
        return calendar.getTime();
    }

    public static String add(Date date, TimeUnit timeUnit, int account,String dateformat) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(timeUnit.getValue(), account);
        Date time = calendar.getTime();
        DateFormat format = new SimpleDateFormat(dateformat);

        return format.format(time);
    }

    /**
     * 获取指定的timestamp
     *
     * @param timeUnit 时间单位
     * @param account  添加到calendarTimeField的时间或日期数量
     * @return
     */
    public static long getTimeInMillis(TimeUnit timeUnit, int account) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(timeUnit.getValue(), account);
        return calendar.getTimeInMillis();
    }

    /**
     * 函数功能描述:UTC时间转本地时间格式
     *
     * @param datetime 日期字符串
     * @return 本地日期
     */
    private static Date parse(String datetime) throws ParseException {
        boolean isUTC = false;
        String utcTimePattern = "yyyy-MM-dd";
        String subTime = datetime.substring(10);// UTC时间格式以 yyyy-MM-dd 开头,将utc时间的前10位截取掉,之后是含有多时区时间格式信息的数据

        // 处理当后缀为:+8:00时,转换为:+08:00 或 -8:00转换为-08:00
        if (subTime.indexOf("+") != -1) {
            subTime = changeUtcSuffix(subTime, "+");
        } else if (subTime.indexOf("-") != -1) {
            subTime = changeUtcSuffix(subTime, "-");
        }
        datetime = datetime.substring(0, 10) + subTime;

        // 依据传入函数的utc时间,得到对应的utc时间格式
        // 步骤一:处理 T
        if (datetime.indexOf("T") != -1) {
            utcTimePattern += "'T'";
        }

        // 步骤二:处理毫秒SSS
        if (StringUtils.hasText(subTime)) {
            if (datetime.indexOf(".") != -1) {
                utcTimePattern = utcTimePattern + "HH:mm:ss.SSS";
            } else if (subTime.indexOf("+") == -1 || subTime.indexOf("-") == -1 || subTime.indexOf("Z") == -1) {
                List<String> list = Arrays.asList("HH:mm:ss".split("[:]"));
                utcTimePattern = utcTimePattern + String.join(":", list.subList(0, subTime.split("[:]").length));
            } else {
                utcTimePattern = utcTimePattern + "HH:mm:ss";
            }
        }

        // 步骤三:处理时区问题
        if (subTime.indexOf("+") != -1 || subTime.indexOf("-") != -1) {
            utcTimePattern += "XXX";
            isUTC = true;
        } else if (subTime.indexOf("Z") != -1) {
            utcTimePattern += "'Z'";
            isUTC = true;
        }


        SimpleDateFormat utcFormater = new SimpleDateFormat(utcTimePattern);
        if (isUTC) {
            utcFormater.setTimeZone(TimeZone.getTimeZone("UTC"));
        }
        Date date = utcFormater.parse(datetime);
        return date;

    }

    /**
     * 函数功能描述:修改时间格式后缀
     * 函数使用场景:处理当后缀为:+8:00时,转换为:+08:00 或 -8:00转换为-08:00
     *
     * @param subTime
     * @param sign
     * @return
     */
    private static String changeUtcSuffix(String subTime, String sign) {
        String timeSuffix = null;
        String[] splitTimeArrayOne = subTime.split("[" + sign + "]");
        String[] splitTimeArrayTwo = splitTimeArrayOne[1].split(":");
        if (splitTimeArrayTwo[0].length() < 2) {
            timeSuffix = sign + "0" + splitTimeArrayTwo[0] + ":" + splitTimeArrayTwo[1];
            subTime = splitTimeArrayOne[0] + timeSuffix;
            return subTime;
        }
        return subTime;
    }

    public static long pastDays(Date date) {
        long t = System.currentTimeMillis() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }

    public static Date addDays(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days); // minus number would decrement the days
        return cal.getTime();
    }

    public static Date localDateToDate(LocalDate localDate) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDate.atStartOfDay(zoneId);

        Date date = Date.from(zdt.toInstant());
        return date;
    }

    public static LocalDate dateToLocalDate(Date date) {
        LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        return localDate;
    }

    public static LocalDate stringToLcalDate(String timeStr, String format) {

        LocalDate parse = LocalDate.parse(timeStr, DateTimeFormatter.ofPattern(format));
        return parse;

    }

    public static Date localDateTimeToDate(LocalDateTime localDateTime) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);

        Date date = Date.from(zdt.toInstant());
        return date;
    }

    public static LocalDateTime dateToLocalDateTime(Date date) {
        LocalDateTime LocalDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return LocalDateTime;
    }

    public static String getRemainingTime(Date future) {
        long seconds = (future.getTime() - System.currentTimeMillis()) / 1000;

        int secondsPerDay = 86400;
        int secondsPerHour = 3600;
        int secondsPerMinute = 60;

        if (seconds < 0) {
            return "-";
        }

        StringBuilder stringBuilder = new StringBuilder();
        if (seconds > secondsPerDay) {
            stringBuilder.append(seconds / secondsPerDay).append("天");
            seconds -= seconds / secondsPerDay * secondsPerDay;
        }


        if (seconds > secondsPerHour) {
            stringBuilder.append(seconds / secondsPerHour).append("时");
            seconds -= seconds / secondsPerHour * secondsPerHour;
        } else if (stringBuilder.length() > 0) {
            stringBuilder.append(0).append("时");
        }

        if (seconds > secondsPerMinute) {
            stringBuilder.append(seconds / secondsPerMinute).append("分");
            seconds -= seconds / secondsPerMinute * secondsPerMinute;
        } else if (stringBuilder.length() > 0) {
            stringBuilder.append(0).append("分");
        }

        stringBuilder.append(seconds).append("秒");
        return stringBuilder.toString();

    }

    public static Long getTimeDifference(Date beginDate, Date endDate, TimeUnit timeUnit) {
        long beginTime = beginDate.getTime();
        long endTime = endDate.getTime();
        int unit = 1;
        if (timeUnit.getValue() == Calendar.DAY_OF_YEAR) {
            unit = 1000 * 60 * 60 * 24;
        }
        // 其他单位待完善
        long betweenDays = (long) ((endTime - beginTime) / unit + 0.5);
        return betweenDays;
    }

    /**
     * 获取当日剩余时间，单位：秒
     *
     * @return
     */
    public static long getTodayRemainingTimeInSecond() {
        Instant end = LocalDate.now().atStartOfDay(ZoneId.systemDefault()).plusDays(1).plus(-1, ChronoUnit.SECONDS)
                               .toInstant();
        long seconds = Duration.between(Instant.now(), end).toMillis() / 1000;
        return seconds;
    }

    /**
     * 获取距离指定日期的剩余时间，单位：秒
     *
     * @return
     */
    public static Integer getRemainSecondsOneDay(LocalDateTime localDateTime) {
        long seconds = ChronoUnit.SECONDS.between(LocalDateTime.now(), localDateTime);
        return (int) seconds;
    }

    /**
     * 获取N日内剩余时间，单位：秒
     *
     * @return
     */
    public static long getDaysRemainingTimeInSecond(Long days) {
        Instant end = LocalDate.now().atStartOfDay(ZoneId.systemDefault()).plusDays(days).plus(-1, ChronoUnit.SECONDS)
                               .toInstant();
        long seconds = Duration.between(Instant.now(), end).toMillis() / 1000;
        return seconds;
    }

    /**
     * 解析字符串为LocalTime
     *
     * @param source
     * @return
     */
    public static LocalTime parseTime(String source) {

        String timePattern = "";

        if (source.indexOf(".") != -1) {
            timePattern = FULL_TIME_PATTERN;
        } else {
            timePattern = FULL_TIME_PATTERN.substring(0, FULL_TIME_PATTERN.indexOf("."));
            timePattern = Joiner.on(":")
                                .join(Arrays.asList(timePattern.split("[:]")).subList(0, source.split("[:]").length));
        }

        return LocalTime.parse(source, DateTimeFormatter.ofPattern(timePattern));
    }

    /**
     * 产生指定日期的随机时间点
     *
     * @param localDate
     * @param startTime
     * @param endTime
     * @return
     */
    public static Date getRandomTime(LocalDate localDate, LocalTime startTime, LocalTime endTime) {
        int offsetSeconds = 0;
        int compared = startTime.compareTo(endTime);
        if (compared != 0) {
            Duration duration = Duration.between(startTime, endTime);
            offsetSeconds = new Random().nextInt((int) Math.abs(duration.toMillis() / 1000));
        }

        LocalTime randomTime = (compared < 0 ? startTime : endTime).plusSeconds(offsetSeconds);
        return Date.from(LocalDateTime.of(localDate, randomTime).atZone(ZoneOffset.systemDefault()).toInstant());
    }

    /**
     * 合并localDate和localTime为Date
     *
     * @param localDate
     * @param localTime
     * @return
     */
    public static Date combine(LocalDate localDate, LocalTime localTime) {
        return Date.from(LocalDateTime.of(localDate, localTime).atZone(ZoneOffset.systemDefault()).toInstant());
    }

    public static LocalTime min(LocalTime a, LocalTime b) {
        return a.isBefore(b) ? a : b;
    }

    public static LocalTime max(LocalTime a, LocalTime b) {
        return a.isAfter(b) ? a : b;
    }

    public static long getMinutesBetween(LocalDateTime a, LocalDateTime b) {
        val duration = Duration.between(a, b);
        return duration.toMinutes();
    }

    public static LocalDateTime getMinLocalDateTime() {
        return LocalDateTime.of(1900, 1, 1, 0, 0, 0);
    }

    public static LocalDate getMinLocalDate() {
        return LocalDate.of(1900, 1, 1);
    }


    public static int compare(LocalDateTime small, LocalDateTime large) {
        return small.isBefore(large) ? -1 : small.equals(large) ? 0 : 1;
    }

    public static String escapeBy(LocalDateTime start, LocalDateTime now) {
        Duration duration = Duration.between(start, now);
        if (duration.toHours() < 1) {
            return "刚刚";
        } else if (duration.toDays() < 1) {
            return String.format("%d小时前", duration.toHours());
        } else {
            return String.format("%d天前", duration.toDays());
        }
    }

    public static LocalDateTime parseStringToDateTime(String dateTime) {
        if (StringUtils.isEmpty(dateTime)) {
            return null;
        }
        LocalDateTime localDateTime = LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATEFORMAT_STANDARD));
        return localDateTime;
    }

    /**
     * 获取当前小时
     *
     * @return
     */
    public static int getHour() {
        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
        return hour;
    }


    public enum TimeUnit {
        YEAR(Calendar.YEAR),
        MONTH(Calendar.MONTH),
        DAY(Calendar.DAY_OF_YEAR),
        HOUR(Calendar.HOUR_OF_DAY),
        MINUTE(Calendar.MINUTE),
        SECOND(Calendar.SECOND),
        MILLISECOND(Calendar.MILLISECOND);


        private int v;

        TimeUnit(int value) {
            this.v = value;
        }

        int getValue() {
            return this.v;
        }
    }

    public static LocalDateTime strToLocalDateTime(String strLocalTime, String dft) {
        // 1.具有转换功能的对象
        DateTimeFormatter df = DateTimeFormatter.ofPattern(dft);
        // 2.要转换的对象
        LocalDateTime time = LocalDateTime.now();

        // 3.发动功能
        String localTime = df.format(time);
        System.out.println("LocalDateTime转成String类型的时间：" + localTime);

        // 3.LocalDate发动，将字符串转换成  df格式的LocalDateTime对象，的功能
        LocalDateTime LocalTime = LocalDateTime.parse(strLocalTime, df);
        return LocalTime;
    }

    public static int getMonth(LocalDate localDate) {
        return localDate.getMonthValue();
    }

    public static int getWeekOfYear(String date,String format) {
        SimpleDateFormat df=new SimpleDateFormat(format);
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(df.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //设置每周第一天为周一 默认每周第一天为周日
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        //获取当前日期所在周周日
        calendar.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }


//    public static void main(String[] args) {
//        System.out.println(DateHelper.getWeekOfYear(DateHelper.serialize(DateHelper.addDays(new Date(),-6),"yyyy-MM-dd"),"yyyy-MM-dd"));
//    }

    public static LocalDate parseLocalDate(String localDateStr, String dateFormat) {
        return LocalDate.parse(localDateStr, DateTimeFormatter.ofPattern(dateFormat));
    }
    public static LocalDate getFirstDayOfMonth(LocalDate localDate){
        return localDate.with(TemporalAdjusters.firstDayOfMonth());
    }
    public static LocalDate getLastDayOfMonth(LocalDate localDate){
        return localDate.with(TemporalAdjusters.lastDayOfMonth());
    }

    /**
     * 获取两时间间隔天数
     * @param statrDate
     * @param endDate
     * @return
     */
    public static Long getDateIntervalDay(String statrDate,String endDate){

        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date star = dft.parse(statrDate);//开始时间
            Date endDay=dft.parse(endDate);//结束时间
            Long starTime=star.getTime();
            Long endTime=endDay.getTime();
            Long num=endTime-starTime;//时间戳相差的毫秒数
            return num/24/60/60/1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }


    //获得前一天对应的月份第一天
    public static String getPerDayMonthFirstDay(String dt,int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date star = null;
        try {
             star = sdf.parse(dt);//开始时间
        } catch (ParseException e) {
            e.printStackTrace();
        }


        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(star);
        rightNow.add(Calendar.DAY_OF_YEAR, -1);
        rightNow.set(Calendar.DAY_OF_MONTH, 1);
        Date dt1 = rightNow.getTime();
        return getDateMonthFirstDay(dt1,-(month - 1));
    }

    public static String getPerDayMonthFirstDay(Date dt,int month) {
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_YEAR, -1);
        rightNow.set(Calendar.DAY_OF_MONTH, 1);
        Date dt1 = rightNow.getTime();
        return getDateMonthFirstDay(dt1,-(month - 1));
    }

    //获得指定月份第一天
    public static String getDateMonthFirstDay(Date dt,int month) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, month);
        rightNow.set(Calendar.DAY_OF_MONTH, 1);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 获取指定日期所属季度
     * @param localDate
     * @return
     */
    public static int  getQuarter(LocalDate localDate){
        int month = localDate.getMonthValue();
        int quarter = (month + 2) / 3;
        System.out.println("当前季度为：" + quarter);
        return quarter;
    }

    /**
     * 获取今年第一天
     * @return
     */
    public static String getFirstDayByYear(){
        Calendar currCal=Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR,currCal.get(Calendar.YEAR));
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(DateHelper.DATEFORMAT_STANDARD);
        String firstday =  format.format(time);
        return firstday;
    }
    public static Boolean isNullOrEmpty(LocalDateTime localDateTime){
        return localDateTime == null || localDateTime.equals(UNIX_START_LOCAL_DATE_TIME);
    }

    /**
     * 获取当前时间前一天零点
     * @return
     */
    public static LocalDateTime getPreDayStart(){
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();

        // 获取当前日期
        LocalDate currentDate = now.toLocalDate();

        // 使用当前日期减去一天得到前一天的日期
        LocalDate previousDate = currentDate.minusDays(1);

        // 构造前一天的零点时间
        LocalDateTime previousDayStart = LocalDateTime.of(previousDate, LocalTime.MIN);

        return previousDayStart;
    }

    public static int getWeekday(String dateStr) {
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 解析字符串为LocalDate对象
        LocalDate date = LocalDate.parse(dateStr, formatter);
        // 获取本周第一天
        LocalDate firstDayOfWeek = date.with(DayOfWeek.MONDAY);
        // 计算与本周第一天相差的天数，加1得到本周第几天（星期一为第一天）
        return date.getDayOfWeek().getValue() - firstDayOfWeek.getDayOfWeek().getValue() + 1;
    }


}
