package com.jkcredit.invoice.util;

import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * @description: 日期时间工具类
 * @author: hyh
 * @create: 2023-01-08 15:58
 **/

public class DateUtil {

    private static String[] parsePatterns = {"yyyy-MM"};

    /**
     * 时间转换毫秒
     *
     * @param time
     * @return
     */
    public static Long date2Java(String time) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format);
        Date date;
        date = dateTime.toDate();
        return date.getTime();
    }

    /**
     * 字符串转时间
     *
     * @param time
     * @return
     */
    public static Date stringToDate(String time) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format);
        Date date;
        date = dateTime.toDate();
        return date;
    }

    /**
     * 根据时间获取月份
     *
     * @param time
     * @return
     */
    public static String getMonth(String time) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM");
        DateTimeFormatter format1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format1);
        return dateTime.toString(format);
    }

    public static String dateFormate(String time) {
        String formatDate = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            formatDate = sdf.format(sdf2.parse(time).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return time;
        }
        return formatDate;
    }

    public static String getCurrentDateStr() {
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf2.format(System.currentTimeMillis());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getCurrentDateStrFormat() {
        return dateFormate(getCurrentDateStr());
    }

    /**
     * 时间格式转换
     * yyyy-MM-dd HH:mm:ss 转 yyyy-MM-dd'T'HH:mm:ss
     *
     * @param time
     * @return
     */
    public static synchronized String dateToTime(String time) {
        //转换前格式
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        //转换后格式
        DateTimeFormatter format1 = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        //将传入时间按照format格式转换为DateTime类型
        DateTime dateTime = DateTime.parse(time, format);
        //转换为format1字符串类型
        return dateTime.toString(format1);
    }

    /**
     * 判断某个时间 是否在某个区间内
     *
     * @param startTime
     * @param endTime
     * @param invalidTime
     * @return
     */
    public static boolean isInvalidTime(String startTime, String endTime, String invalidTime) {
        Date nowDate = null;
        Date beginDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-ddHH:mm:ss");
        try {
            beginDate = sdf.parse(startTime);
            endDate = sdf.parse(endTime);
            nowDate = sdf.parse(invalidTime);
        } catch (Exception e) {
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowDate);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginDate);
        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        return date.after(begin) && date.before(end);
    }

    public static String tTimeToDate(String time) {
        //转换前格式
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss");
        //转换后格式
        DateTimeFormatter format1 = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        //将传入时间按照format格式转换为DateTime类型
        DateTime dateTime = DateTime.parse(time, format);
        //转换为format1字符串类型
        return dateTime.toString(format1);
    }


    public static String tLocalDateTimeToString(LocalDateTime time) {
        //转换后格式
        java.time.format.DateTimeFormatter outputFormatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        //将传入时间按照format格式转换为DateTime类型
        String dateTime = time.format(outputFormatter);
        //转换为format1字符串类型
        return dateTime;
    }

    /**
     * 判断time是否在now的n天之内
     *
     * @param time
     * @param n    正数表示在条件时间n天之后，负数表示在条件时间n天之前
     * @return
     */
    public static boolean belongDate(String time, int n) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(time, format);
        Date inputTime;

        inputTime = dateTime.toDate();

        //得到日历
        Calendar calendar = Calendar.getInstance();
        //把当前时间赋给日历
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, n);
        //得到n前的时间
        Date before7days = calendar.getTime();

        return before7days.getTime() < inputTime.getTime();

    }

    /**
     * 判断给定时间与当前时间相差多少天
     *
     * @param date
     * @return
     */
    public static long getDistanceDays(String date, Date now) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date, format);

        long days = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();

            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            days = diff / (60 * 60 * 24);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return days;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(String smdate, Date bdate) {

        long beTweenDays = 0L;
        try {
            DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
            DateTime dateTime = DateTime.parse(smdate, format);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date smdate1 = dateTime.toDate();
            bdate = sdf.parse(sdf.format(bdate));
            Calendar cal = Calendar.getInstance();
            cal.setTime(smdate1);
            long time1 = cal.getTimeInMillis();
            cal.setTime(bdate);
            long time2 = cal.getTimeInMillis();
            beTweenDays = (time1 - time2) / (1000 * 3600 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return Integer.parseInt(String.valueOf(beTweenDays));
    }

    /**
     * 判断给定时间与当前时间相差多少天
     *
     * @param
     * @return
     */
    public static long getDistanceDays(String date1, String date2) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date1, format);
        DateTime dateTime1 = DateTime.parse(date2, format);
        long days = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();
            Date now = dateTime1.toDate();
            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            days = diff / (60 * 60 * 24 * 1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return days;
    }

    /**
     * 判断给定时间与当前时间相差多少小时
     *
     * @param date
     * @return
     */
    public static long getDistanceHours(String date, Date now) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date, format);

        long hours = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();

            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            hours = diff / (60 * 60 * 1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return hours;
    }

    /**
     * 判断给定时间与当前时间相差多少小时
     *
     * @param date
     * @return
     */
    public static long getDistanceMills(String date, Date now) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date, format);

        long mills = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();

            long time1 = time.getTime();
            long time2 = now.getTime();
            long diff = time1 - time2;
            mills = diff;

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return mills;
    }

    /**
     * 得到几天前的时间
     */

    public static String getDateAfterDays(String date, int day) {

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date dateR = null;
        try {
            dateR = format.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar now = Calendar.getInstance();
        now.setTime(dateR);

        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);

        return format.format(now.getTime());

    }

    /**
     * 得到几天前的时间
     */

    public static String getDateAfterDayTime(String date, int day) {

        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateR = null;
        try {
            dateR = format.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Calendar now = Calendar.getInstance();
        now.setTime(dateR);

        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);

        return format.format(now.getTime());

    }

    /**
     * 判断给定时间与给定时间相差多少小时
     *
     * @param date
     * @return
     */
    public static long getDistanceHoursTwo(String date, String data2) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        DateTime dateTime = DateTime.parse(date, format);

        DateTime dateTime2 = DateTime.parse(data2, format);

        long hours = 0;
        try {
            //String转Date
            Date time = dateTime.toDate();
            Date timeTwo = dateTime2.toDate();

            long time1 = time.getTime();
            long time2 = timeTwo.getTime();
            long diff = time1 - time2;
            hours = diff / (60 * 60 * 1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
        //正数表示在当前时间之后，负数表示在当前时间之前
        return hours;
    }


    public static String formatTime(String dateString) {
        java.time.format.DateTimeFormatter inputFormatter = java.time.format.DateTimeFormatter.ofPattern("yyyy/M/d H:mm:ss");
        LocalDateTime dateTime = LocalDateTime.parse(dateString, inputFormatter);
        java.time.format.DateTimeFormatter outputFormatter = java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");
        String formattedDateTime = dateTime.format(outputFormatter);
        return formattedDateTime;
    }

    public static void main(String[] args) {

       /* System.out.println(getDistanceHoursFormat("2022-11-31T23:59:59"));

        System.out.println(isValidDate("2022-12-01T23:59:59"));
        System.out.println(isValidDate("2022-11-31T23:59:59"));
        System.out.println(getMouthDays("2022-5"));*/
        // System.out.println(DateUtil.getDistanceHoursTwo("2022-02-26 09:02:26","2022-03-01 09:02:58"));
        //System.out.print(getSplitTimeOver96("2023-01-04 12:28:11","2023-01-09 12:28:10"));
    }


    public static String[] getMouthDays(String mounth) {
        DateFormat format = new SimpleDateFormat("yyyy-MM");
        String[] dates = null;
        try {
            Date date = format.parse(mounth);
            Calendar calendar = new GregorianCalendar();
            calendar.setTime(date);
            int minDate = calendar.getActualMinimum(Calendar.DATE);
            int maxDate = calendar.getActualMaximum(Calendar.DATE);
            dates = new String[maxDate];
            for (int i = minDate; i <= maxDate; i++) {
                System.out.print(i + "");
                String dateStr = mounth + String.format("-%02d", i).toUpperCase();
                dates[i - 1] = dateStr;
            }
            System.out.print(dates);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dates;
    }

    public static List<String> getSplitTimeOver96(String startDate, String endDate) {
        List<String> ls = new ArrayList<>();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startTime = format.parse(startDate);
            Date endTime = format.parse(endDate);
            long dTime = 0;
            for (dTime = startTime.getTime(); dTime < endTime.getTime(); ) {
                String str1 = "";
                if (dTime != startTime.getTime()) {
                    str1 = format.format(dTime + 1000);
                } else {
                    str1 = format.format(dTime);
                }
                dTime = dTime + (96 * 60 * 60) * 1000;
                String str2 = format.format(dTime);
                if (dTime > endTime.getTime()) {
                    str2 = endDate;
                }

                ls.add(str1 + "#" + str2);
            }
           /* if(dTime!=endTime.getTime()){
                ls.add(format.format(dTime)+"#"+endDate);
            }*/
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
        }

        return ls;
    }

    /**
     * 判断时间格式化
     *
     * @param date
     * @return
     */
    public static String getDistanceHoursFormat(String date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String stringDate = "";
        try {
            //先将该字符串转换为Date类型：
            Date applyDate = sdf.parse(date);
            stringDate = sdf1.format(applyDate);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return stringDate;
    }

    public static Date parseDate(String str) {
        if (str == null) {
            return null;
        }
        try {
            return DateUtils.parseDate(str, parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }


    public static boolean isValidDate(String str) {
        boolean convertSuccess = true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，
            //比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;
    }


}


