package com.qy.agv.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

public class DateUtil {

    public static String getDateFormat() {
        return Constant.DATE_FORMATE;
    }

    public static String getDateTimeFormat() {
        return Constant.DATETIME_FORMATE;
    }


    /**
     * String 转 Date
     *
     * @param stringTime
     * @param format
     * @return
     */
    public static Date stringToDate(String stringTime, String format) {
        if (StringUtils.isEmptyStr(stringTime))
            return null;
        if (format == null || ("").equals(format)) {
            format = Constant.DATE_FORMATE;
        }
        Date dateTime = null;
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            sdf.setLenient(false);
            dateTime = sdf.parse(stringTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * @param
     * @Description: 验证字符串是否是有效的日期格式
     * @author cxx
     */
    public static boolean isValidDate(String str, String format) {
        boolean convertSuccess = true;
        if (StringUtils.isEmptyStr(str))
            return false;
        if (format == null || ("").equals(format)) {
            format = Constant.DATE_FORMATE;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            // 设置lenient为false.
            // 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            sdf.setLenient(false);
            sdf.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
            // 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess = false;
        }
        return convertSuccess;

    }

    /**
     * 获取当前系统的日期
     *
     * @param dateFormat
     * @return
     */
    public static Date getCurSysDate(String dateFormat) {
        if (dateFormat == null || ("").equals(dateFormat)) {
            dateFormat = DateUtil.getDateFormat();
        }
        Date curDate = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            String strCurDate = sdf.format(new Date());
            curDate = sdf.parse(strCurDate);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return curDate;
    }

    /**
     * Util.Date 转 sql.date
     *
     * @param date
     * @return
     */
    public static java.sql.Date transDateTDate(Date date) {
        java.sql.Date d = new java.sql.Date(date.getTime());
        return d;
    }

    /**
     * Util.Date 转 sql.date
     *
     * @return
     */
    public static java.sql.Date transDateTDate(String stringTime, String format) {
        if (format == null || ("").equals(format)) {
            format = Constant.DATE_FORMATE;
        }
        java.sql.Date d = new java.sql.Date(stringToDate(stringTime, format).getTime());
        return d;
    }

    /**
     * date 转字符串
     *
     * @param date
     * @param formatType ("yyyy-MM-dd HH:mm:ss")
     * @return
     */
    public final static String dateToString(Date date, String formatType) {
        if (date == null) {
            return "";
        }
        if(StringUtils.isEmpty(formatType)){
            formatType = Constant.DATETIME_FORMATE;
        }
        DateFormat dateFormat;
        dateFormat = new SimpleDateFormat(formatType, Locale.CHINA);
        dateFormat.setLenient(false);
        return dateFormat.format(date);
    }

    /**
     * Calendar 转字符串
     *
     * @param calendar
     * @param formatType
     * @return
     */
    public final static String calendarToString(Calendar calendar, String formatType) {
        if (calendar == null) {
            return "";
        }
        SimpleDateFormat dateFormat;
        dateFormat = new SimpleDateFormat(formatType, Locale.CHINA);
        dateFormat.setLenient(false);
        Date date = calendar.getTime();
        return dateToString(date, formatType);
    }

    /**
     * 字符串转Calendar
     *
     * @param stringTime
     * @param format
     * @return
     */
    public final static Calendar stringtoCalendar(String stringTime, String format) {
        if (StringUtils.isEmptyStr(stringTime) || StringUtils.isEmptyStr(format))
            return null;
        Date dateTime = stringToDate(stringTime, format);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        return calendar;
    }

    /**
     * 获取当前系统日期
     *
     * @param dateFormat
     * @return
     */
    public static Calendar getCurSysCalendar(String dateFormat) {
        if (dateFormat == null || ("").equals(dateFormat)) {
            dateFormat = DateUtil.getDateFormat();
        }
        Date curDate = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            String strCurDate = sdf.format(new Date());
            curDate = sdf.parse(strCurDate);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dateToCalendar(curDate);
    }

    /**
     * date 转 Calendar
     *
     * @param date
     * @return
     */
    public static Calendar dateToCalendar(Date date) {
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.setTime(date);
        return cal;

    }

    /**
     * 获取两个时间之间的月份
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<String> getBetweenMonth(Calendar startDate, Calendar endDate) {
        List<String> monthDateList = new ArrayList<String>();
        String edate = calendarToString(endDate, Constant.DATEMONTH_FORMATE);
        while (true) {
            String sdate = calendarToString(startDate, Constant.DATEMONTH_FORMATE);
            monthDateList.add(sdate);
            if (sdate.equals(edate)) {
                return monthDateList;
            }
            startDate.add(Calendar.MONTH, 1);
        }

    }

    /**
     * 获取两个时间之间的日期
     *
     * @return
     */
    public static List<String> getBetweenDate(Calendar startCalendar, Calendar endCalendar) {
        Date startDate = startCalendar.getTime();
        Date endDate = endCalendar.getTime();
        startDate = stringToDate(dateToString(startDate, Constant.DATE_FORMATE), Constant.DATE_FORMATE);
        endDate = stringToDate(dateToString(endDate, Constant.DATE_FORMATE), Constant.DATE_FORMATE);
        List<String> dateList = new ArrayList<String>();
        boolean flag = false;
        while (!flag) {
            dateList.add(dateToString(startDate, Constant.DATE_FORMATE));
            if (startDate.compareTo(endDate) == 0) {
                flag = true;
            } else {
                startDate = addDate(startDate, 1);
                if (startDate.compareTo(endDate) > 0) {
                    flag = true;
                    dateList.add(dateToString(endDate, Constant.DATE_FORMATE));
                } else {
                    flag = false;
                }
            }

        }
        return dateList;
    }

    /**
     * 日期增加
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDate(Date date, int day) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);
        return c.getTime();
    }

    /**
     * 小时增加
     *
     * @param date
     * @return
     */
    public static Date addHour(Date date, Integer hour, String format) {
        String today = DateUtil.dateToString(date, format);
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(sdf.parse(today));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.HOUR_OF_DAY, hour);
        return cal.getTime();
    }

    /**
     * 小时增加
     *
     * @param date
     * @return
     */
    public static Date addMinute(Date date, Integer minute) {
        String today = DateUtil.dateToString(date, Constant.DATETIME_FORMATE);
        SimpleDateFormat sdf = new SimpleDateFormat(Constant.DATETIME_FORMATE);
        Calendar cal = Calendar.getInstance();
        try {
            cal.setTime(sdf.parse(today));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        cal.add(Calendar.MINUTE, minute);
        return cal.getTime();
    }

    /**
     * 月份增加
     *
     * @return
     */
    public static Date addMonth(String dateStr, String monthStr, String dateFormat) {
        if (!StringUtils.isEmpty(dateStr) && !StringUtils.isEmpty(monthStr)) {
            Date date = DateUtil.stringToDate(dateStr, dateFormat);
            String[] month_arr = monthStr.split("\\.");
            int month = Integer.parseInt(month_arr[0]);// 获得保质期

            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MONTH, month);// 计算出增加之后时的日期
            Date targetDate = c.getTime();

            if (month_arr.length > 1) {
                int day = 3 * Integer.parseInt(month_arr[1].substring(0, 1));
                targetDate = DateUtil.addDate(targetDate, day);
            }
            return targetDate;
        } else {
            return null;
        }
    }

    public static Date addYear(Date date, int year) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, year);
        return c.getTime();
    }

    public static long getMillis(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * 获取当天日期字符串 格式为：yyyyMMdd
     *
     * @return 当天日期字符串
     */
    public static String getCurrDate() {
        Date currDate = new Date();

        DateFormat df = new SimpleDateFormat(Constant.DATETIME_FORMATE);
        String strCurrDate = df.format(currDate);

        return strCurrDate;
    }

    /**
     * 获取当前系统日期
     *
     * @param formate
     * @return
     */
    public static String getCurrDate(String formate) {
        Date currDate = new Date();

        DateFormat df = new SimpleDateFormat(formate);
        String strCurrDate = df.format(currDate);

        return strCurrDate;
    }

    /**
     * 当前系统日期是否在date1之后，data2之前
     *
     * @param date1
     * @param date2
     * @param dateFormat
     * @return
     */
    public static boolean isInNowDate(Date date1, Date date2, String dateFormat) {
        if (dateFormat == null || ("").equals(dateFormat)) {
            dateFormat = Constant.DATE_FORMATE;
        }
        Date curDate1 = new Date();
        Date curDate2 = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            String strCurDate1 = sdf.format(date1);
            curDate1 = sdf.parse(strCurDate1);
            String strCurDate2 = sdf.format(date2);
            curDate2 = sdf.parse(strCurDate2);
            if (curDate1.before(getCurSysDate(dateFormat)) && curDate2.after(getCurSysDate(dateFormat)))
                return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * date1是否在date2之前
     *
     * @param date1
     * @param date2
     * @param dateFormat
     * @return
     */
    public static boolean isBeafoerDate(Date date1, Date date2, String dateFormat) {
        if (dateFormat == null || ("").equals(dateFormat)) {
            dateFormat = Constant.DATE_FORMATE;
        }
        Date curDate1 = new Date();
        Date curDate2 = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            String strCurDate1 = sdf.format(date1);
            curDate1 = sdf.parse(strCurDate1);
            String strCurDate2 = sdf.format(date2);
            curDate2 = sdf.parse(strCurDate2);
            if (curDate1.before(curDate2))
                return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * sourDate 是否在date1和date2之间(包含)
     *
     * @param date1
     * @param date2
     * @param sourDate
     * @param dateFormat
     * @return
     */
    public static boolean isBetweenDate(String date1, String date2, String sourDate, String dateFormat) {
        if (dateFormat == null || ("").equals(dateFormat)) {
            dateFormat = Constant.DATE_FORMATE;
        }
        Date curDate1 = new Date();
        Date curDate2 = new Date();
        Date curSourDate = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            curDate1 = sdf.parse(date1);
            curDate2 = sdf.parse(date2);
            curSourDate = sdf.parse(sourDate);
            if ((curDate1.before(curSourDate) || curDate1.equals(curSourDate)) && (curDate2.after(curSourDate) || curDate2.equals(curSourDate)))
                return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * date1是否在date2之前 或同一天
     *
     * @param date1
     * @param date2
     * @param dateFormat
     * @return
     */
    public static boolean isBeafoerEqualsDate(Date date1, Date date2, String dateFormat) {
        if (dateFormat == null || ("").equals(dateFormat)) {
            dateFormat = Constant.DATE_FORMATE;
        }
        Date curDate1 = new Date();
        Date curDate2 = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            String strCurDate1 = sdf.format(date1);
            curDate1 = sdf.parse(strCurDate1);
            String strCurDate2 = sdf.format(date2);
            curDate2 = sdf.parse(strCurDate2);
            if (curDate1.before(curDate2) || curDate1.equals(curDate2))
                return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * date1和date2是否同一天
     *
     * @param date1
     * @param date2
     * @param dateFormat
     * @return
     */
    public static boolean isEqualsDate(Date date1, Date date2, String dateFormat) {
        if (date1 == null || date2 == null) {
            return false;
        }
        if (dateFormat == null || ("").equals(dateFormat)) {
            dateFormat = Constant.DATE_FORMATE;
        }
        Date curDate1 = new Date();
        Date curDate2 = new Date();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            String strCurDate1 = sdf.format(date1);
            curDate1 = sdf.parse(strCurDate1);
            String strCurDate2 = sdf.format(date2);
            curDate2 = sdf.parse(strCurDate2);
            if (curDate1.equals(curDate2))
                return true;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 返回年份
     *
     * @param date 日期
     * @return 返回年份
     */
    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    public static int getHour(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinute(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MINUTE);
    }

    /**
     * 返回月份
     *
     * @param date 日期
     * @return 返回月份
     */
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 返回日份
     *
     * @param date 日期
     * @return 返回日份
     */
    public static int getDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取间隔今天n天的时间点
     *
     * @param i 间隔时间, 如 i=3, 三天后的时间; i=-5, 五天前的时间
     * @return yyyyMMddHHmmss格式的时间串
     */
    public static long getDayCompareToday(int i, String format) {
        Calendar today = getCurSysCalendar(format);
        today.add(Calendar.DAY_OF_YEAR, i);
        return today.getTimeInMillis();
    }

    /**
     * 返回两个时间的相差天数,如果endDate大于startDate,返回负数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static float getBetweenDay(Date startDate, Date endDate) {
        int betweenDays = getDaysBetween(startDate, endDate);
        if (startDate.before(endDate)) {
            betweenDays = betweenDays * -1;
        }
        return betweenDays;
        // label ((startDate.getTime() - endDate.getTime() ) /
        // (24*60*60*1000));
    }

    public static long getBetweenMinute(Date startDate, Date endDate) {
        if(startDate==null || endDate == null){
            return 0;
        }
        long s1 = startDate.getTime();
        long s2 = endDate.getTime();
        long s = s2 - s1;
        return  s / (60*1000);
    }

    /**
     * 返回两个时间的相差天数,如果endDate大于startDate,返回负数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getBetweenDayMy(Date startDate, Date endDate) {
        int betweenDays = getDaysBetween(startDate, endDate);
        if (startDate.before(endDate)) {
            betweenDays = betweenDays * -1;
        }
        return betweenDays;
        // label ((startDate.getTime() - endDate.getTime() ) /
        // (24*60*60*1000));
    }

    /**
     * 计算两个时间的天数间隔，正数
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDaysBetween(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            throw new RuntimeException("参数不能为null");
        }
        Calendar d1 = Calendar.getInstance();
        d1.setTime(startDate);
        Calendar d2 = Calendar.getInstance();
        d2.setTime(endDate);

        if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
            Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }
        int days = d2.get(Calendar.DAY_OF_YEAR) - d1.get(Calendar.DAY_OF_YEAR);
        int y2 = d2.get(Calendar.YEAR);
        if (d1.get(Calendar.YEAR) != y2) {
            d1 = (Calendar) d1.clone();
            do {
                days += d1.getActualMaximum(Calendar.DAY_OF_YEAR);
                d1.add(Calendar.YEAR, 1);
            } while (d1.get(Calendar.YEAR) != y2);
        }
        return days;
    }

    /**
     * 计算两个日期之间的工作日间隔天数
     *
     * @return
     */
    public static int getWorkingDay(Date startDate, Date endDate) {
        if (startDate == null || endDate == null) {
            throw new RuntimeException("参数不能为null");
        }

        int result = -1;
        Calendar d1 = Calendar.getInstance();
        d1.setTime(startDate);
        Calendar d2 = Calendar.getInstance();
        d2.setTime(endDate);

        if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
            Calendar swap = d1;
            d1 = d2;
            d2 = swap;
        }

        // int betweendays = getDaysBetween(startDate, endDate);
        //
        // int charge_date = 0;
        int charge_start_date = 0;// 开始日期的日期偏移量
        int charge_end_date = 0;// 结束日期的日期偏移量
        // 日期不在同一个日期内
        int stmp;
        int etmp;
        stmp = 7 - d1.get(Calendar.DAY_OF_WEEK);
        etmp = 7 - d2.get(Calendar.DAY_OF_WEEK);
        if (stmp != 0 && stmp != 6) {// 开始日期为星期六和星期日时偏移量为0
            charge_start_date = stmp - 1;
        }
        if (etmp != 0 && etmp != 6) {// 结束日期为星期六和星期日时偏移量为0
            charge_end_date = etmp - 1;
        }
        // }
        result = (getDaysBetween(getNextMonday(d1).getTime(), getNextMonday(d2).getTime()) / 7) * 5 + charge_start_date - charge_end_date;
        // System.out.println("charge_start_date>" + charge_start_date);
        // System.out.println("charge_end_date>" + charge_end_date);
        // System.out.println("between day is-->" + betweendays);
        return result;
    }

    public static String getChineseWeek(Calendar date) {
        final String dayNames[] = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

        int dayOfWeek = date.get(Calendar.DAY_OF_WEEK);

        // System.out.println(dayNames[dayOfWeek - 1]);
        return dayNames[dayOfWeek - 1];

    }

    /**
     * 获得日期的下一个星期一的日期
     *
     * @param date
     * @return
     */
    public static Calendar getNextMonday(Calendar date) {
        Calendar result = null;
        result = date;
        do {
            result = (Calendar) result.clone();
            result.add(Calendar.DATE, 1);
        } while (result.get(Calendar.DAY_OF_WEEK) != 2);
        return result;
    }

    public static Calendar getNextCalendar(Calendar date, int Type, int next) {
        Calendar result = null;
        result = date;
        result = (Calendar) result.clone();
        result.add(Type, next);
        return result;
    }

    /**
     * 计算两个时间的之间休息天数
     *
     * @return
     */
    public int getHolidays(Date startDate, Date endDate) {
        return this.getDaysBetween(startDate, endDate) - this.getWorkingDay(startDate, endDate);
    }

    /**
     * 获取月份的天数
     *
     * @param year
     * @param month
     * @return
     */
    public static int getMonthDay(int year, int month) {
        int days = 0; // 存储当月的天数
        boolean isRn;
        /* 判断是否是闰年 */
        if (year % 4 == 0 && !(year % 100 == 0) || year % 400 == 0) { // 判断是否为闰年
            isRn = true; // 闰年
        } else {
            isRn = false;// 平年
        }
        /* 计算当月的天数 */
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 2:
                if (isRn) {
                    days = 29;
                } else {
                    days = 28;
                }
                break;
            default:
                days = 30;
                break;
        }
        return days;
    }

    /**
     * 格式化日期字符串
     *
     * @param date
     * @param formate
     * @return
     */
    public static String formatStringDate(String date, String formate) {
        Date d = DateUtil.stringToDate(date, formate);
        date = DateUtil.dateToString(d, formate);
        return date;
    }

    /**
     * 获得某周的开始日期和结束日期,如果参数为null，将获取本周的时间段
     *
     * @param weekOfYear 本年的第几周，可为null
     * @return Date[0]:开始时间 Date[1]:结束时间
     */
    public static Date[] getStartAndEndDateOfWeek(Integer weekOfYear) {
        Calendar calendar = Calendar.getInstance();
        if (weekOfYear != null) {
            calendar.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        // calendar.set(Calendar.HOUR, 0); 这个会使得小时变成中午12点
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.setFirstDayOfWeek(Calendar.MONDAY); // 设置周一是每周的开始
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        Date startDate = calendar.getTime();
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 4);
        Date endDate = calendar.getTime();
        Date[] dateGroup = new Date[]{startDate, endDate};
        return dateGroup;
    }

    /**
     * 获得某月的开始日期和结束日期，如果参数为nll，将获取本月的时间段
     *
     * @param month 月份，可为null，如果是9月份就传入9
     * @return Date[0]:开始时间 Date[1]:结束时间
     */
    public static Date[] getStartAndEndDateOfMonth(Integer month) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        if (month != null) {
            calendar.set(Calendar.MONTH, month - 1);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.setFirstDayOfWeek(Calendar.MONDAY); // 设置周一是每周的开始
        calendar.set(Calendar.DATE, 1);
        Date startDate = calendar.getTime();
        calendar.set(Calendar.DATE, calendar.getActualMaximum(calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        Date endDate = calendar.getTime();
        Date[] dateGroup = new Date[]{startDate, endDate};
        return dateGroup;
    }

    /**
     * 将日期传入 返回 yyyy-MM-dd 周几
     *
     * @param time
     * @return
     * @author wangzhengxing
     */
    public static String convertWeekByDate(Date time) {
        String s = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 设置时间格式
        String a1 = sdf.format(time);
        Calendar cal = Calendar.getInstance();
        cal.setTime(time);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天

        switch (dayWeek) {
            case 1:
                s = "周日";
                break;
            case 2:
                s = "周一";
                break;
            case 3:
                s = "周二";
                break;
            case 4:
                s = "周三";
                break;
            case 5:
                s = "周四";
                break;
            case 6:
                s = "周五";
                break;
            case 7:
                s = "周六";
                break;

        }
        s = a1 + " " + s;
        return s;

    }

    /**
     * @param date1
     * @param date2
     * @return
     * @Description: 获取两个日期的月份间隔数
     * @author cxx
     * @date 2014年12月29日 上午9:43:16
     */
    public static int calculateMonthIn(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        ;
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        ;
        cal2.setTime(date2);
        int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
        return Math.abs(c);
    }

    /**
     * @param date1
     * @param date2
     * @return
     * @Description: 获取两个日期的月份间隔数
     * @author cxx
     * @date 2014年12月29日 上午9:43:16
     */
    public static int calculateMonthIn(String date1, String date2) {
        Calendar cal1 = Calendar.getInstance();
        ;
        cal1.setTime(stringToDate(date1, null));
        Calendar cal2 = Calendar.getInstance();
        ;
        cal2.setTime(stringToDate(date2, null));
        int c = (cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR)) * 12 + cal1.get(Calendar.MONTH) - cal2.get(Calendar.MONTH);
        return Math.abs(c);
    }

    //  计算两个Date类型时间相差的时长，返回字符串格式-->例如：（ 2年6月23日23时26分18秒 ）
    public static String getTimeDiffString(Date d1, Date d2) throws ParseException {
        //  分别获取传入的两个时间的毫秒值
        long time1 = d1.getTime();
        long time2 = d2.getTime();
        //  计算两个时间所差的秒值，这里使用系统封装的Math类abs（）绝对值方法做处理，这样就可以忽略传参时间的大小顺序，从而一定能得出一个正确的差值
        long diff = Math.abs((time1 - time2) / 1000);//    除以1000求出的是相差的秒值，秒*1000=毫秒
        //  这里提前把换算 年、月、日 等表达式写好，下面方便运算处理
        /*  注：这里就不做闰年平年、30天31天处理了，后续有时间可能会补上  */
        long year = 60 * 60 * 24 * 365;   //   年和秒之间的换算表达式
        long month = 60 * 60 * 24 * 30;   //   月和秒之间的换算表达式
        long day = 60 * 60 * 24;          //   日和秒之间的换算表达式
        long hour = 60 * 60;              //   小时和秒之间的换算表达式
        long minute = 60;                 //   分钟和秒之间的换算表达式
        //  用上面求出的时间差diff（秒）进行运算
        long yearDiff = diff / year;   //  yearDiff求出的值即为相差的年份
        long restYear = diff % year;   //  差值 % 年 取余 求restYear求出的值即为多出来的不到一年的秒值
        //  用多出来不到一年的值来继续进行运算
        long monthDiff = restYear / month;  //  monthDiff求出的值即为相差的月份
        long restMonth = restYear % month;  //  restMonth=多出来不到一个月的秒值
        //  用多出来不到一个月的值来继续进行运算
        long dayDiff = restMonth / day;     //  dayDiff求出的值即为相差的天数
        long restDay = restMonth % day;     //  restDay=多出来不到一天的秒值
        //  用多出来不到一天的值来进行运算
        long hourDiff = restDay / hour;     //  hourDiff求出的值即为相差的小时数
        long restHour = restDay % hour;     //  restHour=多出来不到一个小时的秒值
        //  用多出来不到一个小时的值来进行运算
        long minuteDiff = restHour / minute;    //  minuteDiff求出的值即为相差的天数
        long restMinute = restHour % minute;    //  restMinute=多出来不到一天的秒值
        String str = "";
        if (yearDiff > 0) {
            str += yearDiff + "年";
        }
        if (monthDiff > 0) {
            str += monthDiff + "月";
        }
        if (dayDiff > 0) {
            str += dayDiff + "日";
        }
        if (hourDiff > 0) {
            str += hourDiff + "时";
        }
        if (minuteDiff > 0) {
            str += minuteDiff + "分";
        }
        str += restMinute + "秒";
        //  最后进行字符串拼接
//        String str = yearDiff + "年" + monthDiff + "月" + dayDiff + "日" + hourDiff + "时" + minuteDiff + "分" + restMinute + "秒";
        //  可根据业务需求修改字符串打印的值
        return str;     //  大功告成
    }

    public static void main(String[] arg) {
        System.out.println(getBetweenMinute(Objects.requireNonNull(stringToDate("2015-08-29 22:10:10", Constant.DATETIME_FORMATE))
                , Objects.requireNonNull(stringToDate("2015-08-29 22:11:11", Constant.DATETIME_FORMATE))));
//        System.out.println(addYear(stringToDate("2015-08-29", Constant.DATE_FORMATE), 2));
//        System.out.println("舍掉小数取整:Math.floor(2)=" + (int) Math.floor(2));
//        System.out.println("舍掉小数取整:Math.floor(2.1)=" + (int) Math.floor(2.1));
//        System.out.println("舍掉小数取整:Math.floor(2.5)=" + (int) Math.floor(2.5));
//        System.out.println(getHour(new Date()));
    }

    public static boolean isValidateTime(String curTime, String startTime, String endTime) {
        if (StringUtils.isEmpty(curTime) || StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {
            return false;
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
            Date curDate = sdf.parse(curTime);
            Date startDate = sdf.parse(startTime);
            Date endDate = sdf.parse(endTime);
            if (curDate.before(endDate) && curDate.after(startDate)) {
                return true;
            }
            if (curDate.equals(startDate) || curDate.equals(endDate)) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static int getCurrHourOfDay(){
        Date currentDate = new Date(); // 获取当前时间
        Calendar calendar = Calendar.getInstance(); // 创建Calendar对象
        calendar.setTime(currentDate); // 设置Calendar对象的时间为当前时间
        int hour = calendar.get(Calendar.HOUR_OF_DAY); // 获取当前小时
        return hour;
    }
}
