package com.yc.ship.module.resource.helper;

import com.alibaba.fastjson.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 操作时间工具类
 *
 * @author qsl
 * <p>
 * 2018年01月03日 下午3:42:23
 */
public class DateHelper {
    public static final String CHINESE_DATE_FORAMT_YMD = "yyyy年MM月dd日";
    public static final String CHINAESE_DATETIME_FORMAT_YMDHMS = "yyyy年MM月dd日 HH:mm:ss";
    public static final String CHINAESE_DATETIME_FORMAT_YMDHM = "yyyy年MM月dd日HH时mm分";
    public static final String CHINAESE_WEEK_FORMAT_YW = "xxxx年xx周";
    public static final String CHINAESE_WEEK_FORMAT = "xxxxxx";
    public static final String DEFAULT_DATE_FORMAT_YMD = "yyyy-MM-dd";
    public static final String DEFAULT_DATE_FORMAT_YM = "yyyy-MM";
    public static final String DEFAULT_DATE_FORMAT_MD = "MM-dd";
    public static final String DEFAULT_DATETIME_FORMAT_YMDH = "yyyy-MM-dd HH";
    public static final String DEFAULT_DATETIME_FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_DATETIME_FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
    public static final String DEFAULT_DATETIME_FORMAT_MDH = "MM-dd HH";
    public static final String NOSYMBOL_DATETIME_FORMAT_YMDHMS = "yyyyMMddHHmmss";
    public static final String NOSYMBOL_DATETIME_FORMAT_YMDH = "yyyyMMddHH";
    public static final String NOSYMBOL_DATETIME_FORMAT_YMD = "yyyyMMdd";
    public static final String NOSYMBOL_TIME_FORMAT_HMS = "HH:mm:ss";
    public static final String NOSYMBOL_TIME_FORMAT_HM = "HH:mm";
    public static final String SPRIT_DATETIME_FORMAT_YMDHMS = "yyyy/MM/dd HH:mm:ss";
    public static final String SPRIT_DATETIME_FORMAT_YMDHMSS = "yyyy/MM/dd HH:mm:ss.S";
    public static final String SPRIT_DATETIME_FORMAT_YMDHM = "yyyy/MM/dd HH:mm";
    public static final String SPRIT_DATETIME_FORMAT_YMD = "yyyy/MM/dd";
    public static final String SPRIT_DATETIME_FORMAT_YMDH = "YYYY/MM/dd HH";
    public static final String FLASH_DATETIME_FORMAT_YMDH = "yyyy,MM,dd,HH,mm,ss";
    public static final String DATE_FORMAT_HOUR = "HH:00";
    public static final int ONE_SECOND_MILLIOS = 1000;
    public static final int ONE_MINUTE_MILLIONS = 60000;
    public static final int ONE_HOUR_MILLIONS = 3600000;
    public static final int ONE_DAY_MILLIONS = 86400000;

    /**
     * 时间格式(yyyy-MM-dd)
     */
    public final static String DATE_PATTERN = "yyyy-MM-dd";
    /**
     * 时间格式(yyyy-MM-dd HH:mm:ss)
     */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public final static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    public final static SimpleDateFormat dd = new SimpleDateFormat("dd");
    public static String date_start = " 00:00:00";
    public static String date_end = " 23:59:59";

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date 日期
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date) {
        return format(date, DATE_PATTERN);
    }

    /**
     * 日期格式化 日期格式为：yyyy-MM-dd
     *
     * @param date    日期
     * @param pattern 格式，如：DateUtils.DATE_TIME_PATTERN
     * @return 返回yyyy-MM-dd格式日期
     */
    public static String format(Date date, String pattern) {
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat(pattern);
            return df.format(date);
        }
        return null;
    }

    /**
     * 字符串转换成日期
     *
     * @param strDate 日期字符串
     * @param pattern 日期的格式，如：DateUtils.DATE_TIME_PATTERN
     */
    public static Date stringToDate(String strDate, String pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }

        DateTimeFormatter fmt = DateTimeFormat.forPattern(pattern);
        return fmt.parseLocalDateTime(strDate).toDate();
    }

    /**
     * 根据周数，获取开始日期、结束日期
     *
     * @param week 周期  0本周，-1上周，-2上上周，1下周，2下下周
     * @return 返回date[0]开始日期、date[1]结束日期
     */
    public static Date[] getWeekStartAndEnd(int week) {
        DateTime dateTime = new DateTime();
        LocalDate date = new LocalDate(dateTime.plusWeeks(week));

        date = date.dayOfWeek().withMinimumValue();
        Date beginDate = date.toDate();
        Date endDate = date.plusDays(6).toDate();
        return new Date[]{beginDate, endDate};
    }

    /**
     * 对日期的【秒】进行加/减
     *
     * @param date    日期
     * @param seconds 秒数，负数为减
     * @return 加/减几秒后的日期
     */
    public static Date addDateSeconds(Date date, int seconds) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusSeconds(seconds).toDate();
    }

    /**
     * 对日期的【分钟】进行加/减
     *
     * @param date    日期
     * @param minutes 分钟数，负数为减
     * @return 加/减几分钟后的日期
     */
    public static Date addDateMinutes(Date date, int minutes) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMinutes(minutes).toDate();
    }

    /**
     * 对日期的【小时】进行加/减
     *
     * @param date  日期
     * @param hours 小时数，负数为减
     * @return 加/减几小时后的日期
     */
    public static Date addDateHours(Date date, int hours) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusHours(hours).toDate();
    }

    /**
     * 对日期的【天】进行加/减
     *
     * @param date 日期
     * @param days 天数，负数为减
     * @return 加/减几天后的日期
     */
    public static Date addDateDays(Date date, int days) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusDays(days).toDate();
    }

    /**
     * 对日期的【周】进行加/减
     *
     * @param date  日期
     * @param weeks 周数，负数为减
     * @return 加/减几周后的日期
     */
    public static Date addDateWeeks(Date date, int weeks) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusWeeks(weeks).toDate();
    }

    /**
     * 对日期的【月】进行加/减
     *
     * @param date   日期
     * @param months 月数，负数为减
     * @return 加/减几月后的日期
     */
    public static Date addDateMonths(Date date, int months) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusMonths(months).toDate();
    }

    /**
     * 对日期的【年】进行加/减
     *
     * @param date  日期
     * @param years 年数，负数为减
     * @return 加/减几年后的日期
     */
    public static Date addDateYears(Date date, int years) {
        DateTime dateTime = new DateTime(date);
        return dateTime.plusYears(years).toDate();
    }

    public static String formatDateToString(Date date, String format) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            return formatter.format(date);
        }
    }

    public static String formateToString(String date, String format) throws Exception {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        Date d = formatter.parse(date);
        return formatDateToString(d, format);
    }

    public static String formatDuration(long time, String format) {
        String result = new String(format);
        int hour = (int) (time / 3600000L);
        int minute = (int) ((time - 3600000L) / 60000L);
        int second = (int) ((time - 3600000L - (long) (minute * '\uea60')) / 1000L);
        int millisecond = (int) (time % 1000L);
        result = result.replace("hh", String.valueOf(hour));
        result = result.replace("mm", String.valueOf(minute));
        result = result.replace("ss", String.valueOf(second));
        result = result.replace("SSS", String.valueOf(millisecond));
        return result;
    }

    public static java.sql.Date toSqlDate(Date date) {
        return date != null ? new java.sql.Date(date.getTime()) : null;
    }

    public static Timestamp toTimestamp(Date date) {
        return date != null ? new Timestamp(date.getTime()) : null;
    }

    public static Date parseDate(String string, String format) {
        Date date = null;

        try {
            if (string != null) {
                SimpleDateFormat ex = new SimpleDateFormat(format);
                date = ex.parse(string);
            }
        } catch (Exception var4) {
            ;
        }

        return date;
    }

    public static Date parseDate(String string) {
        String format = "yyyy-MM-dd";
        if (string.matches("\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}")) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        if (string.matches("\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}:\\d{1,2}.0")) {
            format = "yyyy-MM-dd HH:mm:ss";
        }

        if (string.matches("\\d{2,4}\\-\\d{1,2}\\-\\d{1,2} \\d{1,2}:\\d{1,2}")) {
            format = "yyyy-MM-dd HH:mm";
        }

        return parseDate(string, format);
    }

    public static Date parseStringToDate(String string) {
        Date date = null;
        date = parseDate(string, "yyyy-MM-dd");
        if (date == null) {
            date = parseDate(string, "yyyy-MM-dd HH:mm");
        }

        if (date == null) {
            date = parseDate(string, "yyyy-MM-dd HH:mm:ss");
        }

        if (date == null) {
            date = parseDate(string, "yyyyMMddHHmmss");
        }

        if (date == null) {
            date = parseDate(string, "yyyy/MM/dd HH:mm:ss");
        }

        if (date == null) {
            date = parseDate(string, "yyyy/MM/dd HH:mm");
        }

        if (date == null) {
            date = parseDate(string, "yyyy/MM/dd");
        }

        return date;
    }

    public static int compareDate(String s1, String s2) {
        try {
            Date e = parseDate(s1);
            Date d2 = parseDate(s2);
            return e.compareTo(d2);
        } catch (Exception var4) {
            var4.printStackTrace();
            return -1;
        }
    }

    public static int compareDate(Date d1, Date d2) {
        try {
            return d1.compareTo(d2);
        } catch (Exception var3) {
            return -1;
        }
    }

    private static Calendar getCalendar(long time) {
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(time);
        return cal;
    }

    public static double getDiffInSecond(Date date1, Date date2) {
        long diff = date2.getTime() - date1.getTime();
        return MathHelper.round((double) diff * 1.0D / 1000.0D, 2);
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param begin 较小的时间
     * @param end   较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date begin, Date end) {
        Calendar beginCalendar = Calendar.getInstance();
        beginCalendar.setTime(begin);
        beginCalendar.set(11, 12);
        beginCalendar.set(12, 0);
        beginCalendar.set(13, 0);
        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(end);
        endCalendar.set(11, 12);
        endCalendar.set(12, 0);
        endCalendar.set(13, 0);
        return (int) ((endCalendar.getTimeInMillis() - beginCalendar.getTimeInMillis()) / 86400000L);
    }

    public static long minutesBetween(Date begin, Date end) {
        // 计算时间差（毫秒数）
        long differenceInMillis = Math.abs(end.getTime() - begin.getTime());
        // 将毫秒转换为分钟
        return TimeUnit.MINUTES.convert(differenceInMillis, TimeUnit.MILLISECONDS);
    }


    public static int getYear(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(1);
    }

    public static int getMonth(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(2) + 1;
    }

    public static int getDate(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(5);
    }

    public static int getHour(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(11);
    }

    public static int getMinute(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(12);
    }

    public static int getSecond(Date date) {
        Calendar cal = getCalendar(date.getTime());
        return cal.get(13);
    }

    public static String getCurrDateTime(String pattern) {
        Date utilDate = new Date(System.currentTimeMillis());
        SimpleDateFormat df = new SimpleDateFormat(pattern);
        return df.format(utilDate);
    }

    public static Date[] getAreaOfWeek(int year, int week) {
        Date dt1 = null;
        Date dt2 = null;
        Date[] dates = new Date[2];
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        cal.set(year, 0, 1, 0, 0, 0);
        int maxDay = cal.getActualMaximum(6);

        for (int i = 0; i < maxDay; ++i) {
            if (cal.get(3) == week) {
                if (dt1 == null) {
                    dt1 = cal.getTime();
                }

                dt2 = cal.getTime();
            }

            if (cal.get(3) > week) {
                break;
            }

            cal.add(6, 1);
        }

        dates[0] = dt1;
        dates[1] = dt2;
        return dates;
    }

    public static boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 ? true : year % 100 == 0 && year % 400 == 0;
    }

    public static String getNDayBeforeDateStr(int nday, String pattern) {
        long Current_Time_Millions = System.currentTimeMillis();
        SimpleDateFormat datetimeFormat = new SimpleDateFormat(pattern);
        String nDayBefore = datetimeFormat.format(new Date(Current_Time_Millions - (long) (86400000 * nday)));
        return nDayBefore;
    }

    public static String getNDayBeforeDateStr(int nday) {
        return getNDayBeforeDateStr(nday, "yyyy年MM月dd日 HH:mm:ss");
    }

    public static String getNDayBeforeOnlyDateStr(int nday) {
        return getNDayBeforeDateStr(nday, "yyyy年MM月dd日");
    }

    public static String getAfterDay(String date, int aday, String pattern) throws Exception {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        Date d = dateFormat.parse(date);
        return dateFormat.format(new Date(d.getTime() + (long) (86400000 * aday)));
    }

    public static String getDayOfWeek(String dateStr) {
        Date date = null;
        Calendar calendar = null;
        String dayOfWeek = "";
        if (dateStr.lastIndexOf(" ") == -1) {
            dateStr = dateStr + " 00:00:00";
        }

        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            date = e.parse(dateStr);
            calendar = Calendar.getInstance();
            calendar.setTime(date);
            switch (calendar.get(7) - 1) {
                case 0:
                    dayOfWeek = "星期日";
                    break;
                case 1:
                    dayOfWeek = "星期一";
                    break;
                case 2:
                    dayOfWeek = "星期二";
                    break;
                case 3:
                    dayOfWeek = "星期三";
                    break;
                case 4:
                    dayOfWeek = "星期四";
                    break;
                case 5:
                    dayOfWeek = "星期五";
                    break;
                case 6:
                    dayOfWeek = "星期六";
                    break;
                default:
                    dayOfWeek = "星期日";
            }
        } catch (ParseException var5) {
            var5.printStackTrace();
        }

        return dayOfWeek;
    }

    public static String getDistanceDate(String date, int monthCount, String pattern) throws Exception {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(parseDate(date, pattern));
        return formatDateToString(getAfterMonth(gc, monthCount), pattern);
    }

    public static String getDistanceDate(String date, int monthCount) throws Exception {
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(parseDate(date));
        return formatDateToString(getAfterMonth(gc, monthCount), "yyyy年MM月dd日");
    }

    private static Date getAfterMonth(GregorianCalendar g, int monthNum) {
        GregorianCalendar gc = (GregorianCalendar) g.clone();
        gc.add(2, monthNum);
        return gc.getTime();
    }

    public static int getDateYearDiff(Date dates) {
        if (dates != null) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dates);
            int yearBirth = cal.get(1);
            Calendar cal2 = Calendar.getInstance();
            int yearNow = cal2.get(1);
            cal2.set(1, yearBirth);
            return cal2.before(cal) ? yearNow - yearBirth - 1 : yearNow - yearBirth;
        } else {
            return 0;
        }
    }

    public static Date getBeforeYear(int nyear) {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(1) - nyear;
        cal.set(1, year);
        return cal.getTime();
    }

    public static String getTimeInterval(long messageTime) {
        long interval = (new Date()).getTime() - messageTime;
        long day = interval / 86400000L;
        String time = null;
        if (day >= 0L && day < 1L) {
            int hour = (int) interval / 3600000;
            switch (hour) {
                case 0:
                    time = interval / 60000L + "分钟前";
                    break;
                case 1:
                    time = "一小时前";
                    break;
                case 2:
                    time = "两小时前";
                    break;
                case 3:
                    time = "三小时前";
                    break;
                case 4:
                    time = "四小时前";
                    break;
                case 5:
                    time = "五小时前";
                    break;
                case 6:
                    time = "六小时前";
                    break;
                case 7:
                    time = "七小时前";
                    break;
                case 8:
                    time = "八小时前";
                    break;
                case 9:
                    time = "九小时前";
                    break;
                case 10:
                    time = "十小时前";
                    break;
                case 11:
                    time = "十一小时前";
                    break;
                case 12:
                    time = "十二小时前";
                    break;
                case 13:
                    time = "十三小时前";
                    break;
                case 14:
                    time = "十四小时前";
                    break;
                case 15:
                    time = "十五小时前";
                    break;
                case 16:
                    time = "十六小时前";
                    break;
                case 17:
                    time = "十七小时前";
                    break;
                case 18:
                    time = "十八小时前";
                    break;
                case 19:
                    time = "十九小时前";
                    break;
                case 20:
                    time = "二十小时前";
                    break;
                case 21:
                    time = "二一小时前";
                    break;
                case 22:
                    time = "二十二小时前";
                    break;
                case 23:
                    time = "二十三小时前";
            }
        } else if (day >= 1L && day < 30L) {
            time = interval / 86400000L + "天前";
        } else if (day >= 30L && day < 60L) {
            time = "一个月前";
        } else if (day >= 60L && day < 90L) {
            time = "二个月前";
        } else if (day >= 90L && day < 120L) {
            time = "三个月前";
        } else if (day >= 120L) {
            time = "更早以前";
        }

        return time;
    }

    public static int getCurrentMonthDay() {
        Calendar a = Calendar.getInstance();
        a.set(5, 1);
        a.roll(5, -1);
        int maxDate = a.get(5);
        return maxDate;
    }

    public static int getDaysByYearMonth(int year, int month) {
        Calendar a = Calendar.getInstance();
        a.set(1, year);
        a.set(2, month - 1);
        a.set(5, 1);
        a.roll(5, -1);
        int maxDate = a.get(5);
        return maxDate;
    }

    public static String getDayOfWeekByDate(String date) {
        String dayOfweek = "-1";

        try {
            SimpleDateFormat e = new SimpleDateFormat("yyyy-MM-dd");
            Date myDate = e.parse(date);
            SimpleDateFormat formatter = new SimpleDateFormat("E");
            String str = formatter.format(myDate);
            dayOfweek = str;
        } catch (Exception var6) {
            System.out.println("错误!");
        }

        return dayOfweek;
    }

    public static List<Date> getDates(int workDayCount) {
        ArrayList<Date> dates = new ArrayList<Date>();
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());

        for (; dates.size() < workDayCount; cal.add(5, 1)) {
            int day = cal.get(7);
            if (day != 1 && day != 7) {
                dates.add((Date) cal.getTime().clone());
            }
        }

        return dates;
    }

    public static String formatByMillis(long millis, String pattern) {
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(millis);
        return formatter.format(calendar.getTime());
    }

    public static Date getDateFromMonthFirstDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(5, c.getActualMinimum(5));
        return c.getTime();
    }

    public static Date getDateFromMonthLastDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(5, c.getActualMaximum(5));
        return c.getTime();
    }

    public static boolean isTimeBetweenTime(Date date, Date beginDate, Date endDate) {
        boolean isBetween = false;
        if (date != null) {
            Calendar currentCalendar = Calendar.getInstance();
            currentCalendar.setTime(date);
            currentCalendar.set(2014, 1, 1);
            Calendar beginCalendar = Calendar.getInstance();
            beginCalendar.setTime(beginDate);
            beginCalendar.set(2014, 1, 1);
            Calendar endCalendar = Calendar.getInstance();
            endCalendar.setTime(endDate);
            endCalendar.set(2014, 1, 1);
            if (currentCalendar.getTime().after(beginCalendar.getTime()) && currentCalendar.getTime().before(endCalendar.getTime())) {
                isBetween = true;
            }
        }

        return isBetween;
    }

    public static boolean before(Date startTime, Date endTime) {
        if (startTime != null && endTime != null) {
            Calendar start_cal = Calendar.getInstance();
            start_cal.setTime(startTime);
            Calendar end_cal = Calendar.getInstance();
            end_cal.setTime(endTime);
            return start_cal.before(end_cal);
        } else {
            return false;
        }
    }

    private static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public final static SimpleDateFormat datePattern = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    /**
     * 和当前时间比较大小
     *
     * @param pattern -- 时间格式
     * @param date    -- 需要比较时间
     * @return -1,0,1
     * @throws ParseException
     */
    public static int compareToNow(String pattern, String date)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Calendar ca = Calendar.getInstance();
        Calendar ca1 = Calendar.getInstance();
        ca.setTime(format.parse(date));
        ca1.setTime(format.parse(getNowDate(pattern)));
        return ca.compareTo(ca1);
    }

    /**
     * 两个时间相比较
     *
     * @param pattern -- 时间格式
     * @param date1
     * @param date2
     * @return -1,0,1
     * @throws ParseException
     */
    public static int dateCompare(String pattern, String date1, String date2)
            throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Calendar ca = Calendar.getInstance();
        Calendar ca1 = Calendar.getInstance();
        ca.setTime(format.parse(date1));
        ca1.setTime(format.parse(date2));
        return ca.compareTo(ca1);
    }

    /**
     * 判断某个时间是否在某个范围之内（包含等于）
     *
     * @param pattern -- 时间格式
     * @param minDte  -- 范围的最小值
     * @param maxDate -- 范围的最大值
     * @param date    -- 需要判断的值,now(不区分大小)表示当前时间
     * @return
     * @throws ParseException
     */
    public static boolean compareToScope(String pattern, String minDte,
                                         String maxDate, String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String strDate = "";
        Calendar ca = Calendar.getInstance();
        Calendar ca1 = Calendar.getInstance();
        if ("now".equalsIgnoreCase(date)) {
            strDate = getNowDate(pattern);
        } else {
            strDate = date;
        }
        ca.setTime(format.parse(minDte));
        ca1.setTime(format.parse(strDate));
        int min = ca.compareTo(ca1);
        ca.setTime(format.parse(strDate));
        ca1.setTime(format.parse(maxDate));
        int max = ca.compareTo(ca1);
        if ((min == 0 || min == -1) && (max == 0 || max == -1)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断某个时间是否在某个范围之内（包含等于）
     *
     * @param pattern -- 时间格式
     * @param minDate -- 范围的最小值
     * @param maxDate -- 范围的最大值
     * @param date    -- 需要判断的值,now(不区分大小)表示当前时间
     * @return
     * @throws ParseException
     */
    public static boolean compareToScope(String pattern, Date minDate,
                                         Date maxDate, String date) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        String strDate = "";
        Calendar ca = Calendar.getInstance();
        Calendar ca1 = Calendar.getInstance();
        if ("now".equalsIgnoreCase(date)) {
            strDate = getNowDate(pattern);
        } else {
            strDate = date;
        }
        ca.setTime(minDate);
        ca1.setTime(format.parse(strDate));
        int min = ca.compareTo(ca1);
        ca.setTime(format.parse(strDate));
        ca1.setTime(maxDate);
        int max = ca.compareTo(ca1);
        if ((min == 0 || min == -1) && (max == 0 || max == -1)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断某个时间是否在某个范围之内（包含等于）
     *
     * @param minDate -- 范围的最小值
     * @param maxDate -- 范围的最大值
     * @param date    -- 需要判断的值,now(不区分大小)表示当前时间
     * @return
     * @throws ParseException
     */
    public static boolean compareToScope(Date minDate, Date maxDate, Date date)
            throws ParseException {
        Calendar ca = Calendar.getInstance();
        Calendar ca1 = Calendar.getInstance();
        ca.setTime(minDate);
        ca1.setTime(date);
        int min = ca.compareTo(ca1);
        ca.setTime(date);
        ca1.setTime(maxDate);
        int max = ca.compareTo(ca1);
        if ((min == 0 || min == -1) && (max == 0 || max == -1)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获得当前时间
     *
     * @param pattern -- 时间格式
     * @return
     */
    public static String getNowDate(String pattern) {
        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(new Date());
    }

    /**
     * 日期格式化 yyyy-MM-dd HH:mm:ss
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String getNowDate(Date date, String pattern) {
        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 日期转换成星期几
     *
     * @param date
     * @return
     */
    public static String getWeekByDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int weekday = c.get(Calendar.DAY_OF_WEEK);
        String dayOfWeek = "";
        switch (weekday - 1) {
            case 0:
                dayOfWeek = "星期日";
                break;
            case 1:
                dayOfWeek = "星期一";
                break;
            case 2:
                dayOfWeek = "星期二";
                break;
            case 3:
                dayOfWeek = "星期三";
                break;
            case 4:
                dayOfWeek = "星期四";
                break;
            case 5:
                dayOfWeek = "星期五";
                break;
            case 6:
                dayOfWeek = "星期六";
                break;
            default:
        }
        return dayOfWeek;
    }

    /**
     * 把时间转化为自定义格式
     *
     * @param pattern
     * @param date
     * @return
     */
    public static String dateToString(String pattern, Date date) {
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    /**
     * 将时间转为int
     *
     * @param date
     * @return
     */
    public static int convertDateToInt(Date date) {
        return (int) (date.getTime() / 1000);
    }

    /**
     * 把Int转为Date
     *
     * @param time
     * @return
     */
    public static Date convertIntToDate(Integer time) {
        return new Date((long) time * 1000);
    }

    /**
     * 自定义格式把String转为Date类型
     *
     * @param pattern
     * @param date
     * @return
     * @throws ParseException
     */
    public static Date convertStringToDate(String pattern, String date)
            throws ParseException {

        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        return format.parse(date);
    }

    /**
     * 获取相对于当前时间的任意时间
     *
     * @param date
     * @param interval 小时
     * @throws ParseException
     */
    public static Date getAnyTime(Date date, int interval)
            throws ParseException {
        Calendar cl = Calendar.getInstance();
        cl.setTime(date);
        cl.add(Calendar.HOUR_OF_DAY, interval);
        return cl.getTime();

    }

    /**
     * 对两个时间进行拼接
     *
     * @param date1    -- 时间1
     * @param pattern1 -- 时间1的格式
     * @param date2    -- 时间2
     * @param pattern2 --时间2的格式
     * @return date1+date2
     */
    public static String DateJoin(Date date1, String pattern1, Date date2,
                                  String pattern2) {
        SimpleDateFormat format1 = new SimpleDateFormat(pattern1);
        SimpleDateFormat format2 = new SimpleDateFormat(pattern2);
        String str = format1.format(date1) + " " + format2.format(date2);
        return str;
    }

    /**
     * 返回date1到date2之间所有为星期week-1的时间
     *
     * @param date1
     * @param date2
     * @param week  -- 周几（Calendar.WEDNESDAY，TUESDAY...）
     * @return
     * @throws ParseException
     */
    public static List<Date> getDateOfWeek(Date date1, Date date2, int week)
            throws ParseException {
        int day = (int) ((date2.getTime() - date1.getTime()) / (1000 * 60 * 60 * 24)) + 1;
        Calendar cal = Calendar.getInstance();
        List<Date> dates = new ArrayList<Date>();
        for (int i = 0; i < day; i++) {
            Date date = getAnyTime(date1, 24 * i);
            cal.setTime(date);
            if (cal.get(Calendar.DAY_OF_WEEK) == week) {
                dates.add(date);
            }
        }
        return dates;
    }

    /**
     * 获取 firstDate 到 lastDate 之间每个月的 start号和end号
     * (start,end的范围为1-31，如果某月不足31那么最大为当月的最后一天)
     *
     * @param firstDate
     * @param lastDate
     * @param start
     * @param end
     * @return
     * @throws ParseException
     */
    public static Map<Date, Date> getDateOfMonth(Date firstDate, Date lastDate,
                                                 int start, int end) throws ParseException {
        Map<Date, Date> map = new LinkedHashMap<Date, Date>();
        Calendar cal = Calendar.getInstance();
        Date startDate = null;
        Date endDate = null;
        int min = start >= end ? end : start;
        int max = start >= end ? start : end;
        cal.setTime(firstDate);
        if (min < cal.getActualMinimum(Calendar.DAY_OF_MONTH)) {
            min = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        }
        if (max > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
            max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        cal.set(Calendar.DAY_OF_MONTH, min);
        startDate = cal.getTime();
        cal.set(Calendar.DAY_OF_MONTH, max);
        endDate = cal.getTime();
        map.put(startDate, endDate);
        while (true) {
            min = start >= end ? end : start;
            max = start >= end ? start : end;
            cal.set(Calendar.DAY_OF_MONTH,
                    cal.getActualMaximum(Calendar.DAY_OF_MONTH) + 1);
            Date temp = cal.getTime();
            cal.setTime(temp);
            if (min < cal.getActualMinimum(Calendar.DAY_OF_MONTH)) {
                min = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
            }
            if (max > cal.getActualMaximum(Calendar.DAY_OF_MONTH)) {
                max = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
            }
            cal.set(Calendar.DAY_OF_MONTH, min);
            startDate = cal.getTime();
            cal.set(Calendar.DAY_OF_MONTH, max);
            endDate = cal.getTime();
            if (compareToScope(firstDate, lastDate, startDate)) {
                map.put(startDate, endDate);
            } else {
                break;
            }
        }
        return map;
    }

    /**
     * 取得时间，精确到分，秒为0
     *
     * @param date
     * @return
     */
    public static Integer getTimeInMinute(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.SECOND, 0);
        return convertDateToInt(c.getTime());
    }

    /**
     * 把String时间类型转为Integer
     *
     * @param pattern
     * @param date
     * @return
     * @throws ParseException
     */
    public static Integer convertStrDateToInt(String pattern, String date)
            throws ParseException {
        if (StringUtils.isBlank(pattern)) {
            pattern = DEFAULT_PATTERN;
        }
        Date d = convertStringToDate(pattern, date);
        return convertDateToInt(d);
    }

    /**
     * 把Integer时间类型转为String
     *
     * @param pattern
     * @param date
     * @return
     * @throws ParseException
     */
    public static String convertIntDateToStr(String pattern, Integer date)
            throws ParseException {
        if (pattern == null || "".equals(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        Date d = convertIntToDate(date);
        return dateToString(pattern, d);
    }

    /**
     * 取得当前时间戳
     *
     * @return
     */
    public static Integer nowTimestamp() {
        return convertDateToInt(new Date());
    }

    /**
     * 取得当前日期字符串
     *
     * @return
     */
    public static String nowDateStr() {
        return datePattern.format(new Date());
    }

    /**
     * 获取当天string
     */
    public static String getCrrDay(Integer day) {
        Calendar cal = Calendar.getInstance();
        if (day != null) {
            cal.add(Calendar.DATE, day);
        }
        String dateStart = df.format(cal.getTime());
        return dateStart;
    }

    public static Date getCrrDayDate(Integer day) {
        Calendar cal = Calendar.getInstance();
        if (day != null) {
            cal.add(Calendar.DATE, day);
        }
        return cal.getTime();
    }

    public static String getCrrDay(Calendar cal, Integer day) {
        Calendar clone = (Calendar) cal.clone();
        if (day != null) {
            clone.add(Calendar.DATE, day);
        }
        String dateStart = df.format(clone.getTime());
        return dateStart;
    }

    public static Date getCrrDayDate(Calendar cal, Integer day) {
        Calendar clone = (Calendar) cal.clone();
        if (day != null) {
            clone.add(Calendar.DATE, day);
        }
        return cal.getTime();
    }

    public static String getCrrDayStart(Integer day) {
        Calendar cal = Calendar.getInstance();
        if (day != null) {
            cal.add(Calendar.DATE, day);
        }
        String dateStart = df.format(cal.getTime()) + date_start;
        return dateStart;
    }

    public static Date getCrrDayStartDate(Integer day) {
        Calendar cal = Calendar.getInstance();
        if (day != null) {
            cal.add(Calendar.DATE, day);
            cal.set(Calendar.HOUR_OF_DAY, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
        }
        return cal.getTime();
    }

    public static String getCrrDayEnd(Integer day) {
        Calendar cal = Calendar.getInstance();
        if (day != null) {
            cal.add(Calendar.DATE, day);
        }
        String dateStart = df.format(cal.getTime()) + date_end;
        return dateStart;
    }

    public static Date getCrrDayEndDate(Integer day) {
        Calendar cal = Calendar.getInstance();
        if (day != null) {
            cal.add(Calendar.DATE, day);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 0);
        }
        return cal.getTime();
    }

    /**
     * 获取当年的第一天
     */
    public static String getCrrYearFirstDay() {
        Calendar cal = Calendar.getInstance();
        // 获取今年第一天时间
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        String dateStart = df.format(calendar.getTime());
        return dateStart;
    }

    /**
     * 当月第一天
     *
     * @return
     */
    public static String getCrrMonthFirstDay() {
        Calendar cal_1 = Calendar.getInstance();// 获取当前日期
        cal_1.set(Calendar.DAY_OF_MONTH, 1);// 设置为1号,当前日期既为本月第一天
        String oneDate = df.format(cal_1.getTime());
        return oneDate;
    }

    /**
     * 当月最后一天
     *
     * @return
     */
    public static String getCrrMonthLastDay() {
        Calendar cal_1 = Calendar.getInstance();
        cal_1.set(Calendar.DAY_OF_MONTH, cal_1.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDate = df.format(cal_1.getTime());
        return lastDate;
    }

    /**
     * 当月最后一天
     *
     * @return
     */
    public static String getCrrMonthLastDay(Calendar cal) {
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDate = dd.format(cal.getTime());
        return lastDate;
    }

    /**
     * 上年同月第一天
     *
     * @return
     */
    public static String getLastYearCrrMonthOneDay() {
        Calendar cal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        cal.add(Calendar.YEAR, -1);
        // String yearDateStart = df.format(cal.getTime());
        // System.com.wisesoft.data.out.println(yearDateStart);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        String format = df.format(cal.getTime());
        return format;

    }

    /**
     * 上年同月最后一天
     *
     * @return
     */
    public static String getLastYearCrrMonthLastDay() {
        Calendar cal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR));
        cal.add(Calendar.YEAR, -1);

        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        String lastDate = df.format(cal.getTime());
        return lastDate;

    }

    /**
     * 去年最后一天（到当天）
     *
     * @return
     */
    public static String getLastYearlastDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        String yearDateEnd = df.format(calendar.getTime());
        return yearDateEnd;
    }

    /**
     * 获取当月天数
     *
     * @return
     */
    public static int getCrrMonthSize() {
        Calendar cal = Calendar.getInstance();
        int dateOfMonth = cal.getActualMaximum(Calendar.DATE);
        return dateOfMonth;
    }

    /**
     * yearBefore年第一天
     *
     * @param yearBefore
     * @return
     */
    public static String getYearBrforeFirstDay(int yearBefore) {
        Calendar cal = Calendar.getInstance();
        // 获取今年第一天时间
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR) + yearBefore);
        String dateStart = df.format(calendar.getTime());
        return dateStart;
    }

    /**
     * yearBefore年最后一天（到当天）
     *
     * @param yearBefore
     * @return
     */
    public static String getYearBrforeLastDay(int yearBefore) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, yearBefore);
        String yearDateEnd = df.format(calendar.getTime());
        return yearDateEnd;
    }

    public static String getYearBrforeLastDay(Calendar calendar, int yearBefore) {
        Calendar clone = (Calendar) calendar.clone();
        clone.add(Calendar.YEAR, yearBefore);
        String yearDateEnd = df.format(clone.getTime());
        return yearDateEnd;
    }

    /**
     * 获取当前年份
     *
     * @return
     */
    public static String getNowYear() {
        Calendar calendar = Calendar.getInstance();
        String nowyear = String.valueOf(calendar.get(Calendar.YEAR));
        return nowyear;
    }

    /**
     * 去年第一天
     *
     * @return
     */
    public static String getLastYearFirstDay() {
        Calendar cal = Calendar.getInstance();
        // 获取今年第一天时间
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, cal.get(Calendar.YEAR) - 1);
        String dateStart = df.format(calendar.getTime());
        return dateStart;
    }

    /**
     * 取得指定日期所在周的第一天
     *
     * @return
     */
    public static String getFirstDayOfWeek(Calendar cal) {
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(cal.getTime());
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek()); // Monday
        String dateStart = df.format(cal.getTime());
        return dateStart;
    }

    /**
     * 取得指定日期所在周的最后一天
     *
     * @return
     */
    public static String getLastDayOfWeek(Calendar cal) {
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(cal.getTime());
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek() + 6); // Sunday
        cal.add(Calendar.DATE, 1);//sql包含enddate
        String dateStart = df.format(cal.getTime());
        return dateStart;
    }

    public static String getDate(String unixDate) {

        SimpleDateFormat fm1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long unixLong = 0;
        String date = "";
        try {
            unixLong = Long.parseLong(unixDate) * 1000;
            date = fm1.format(unixLong);
        } catch (Exception ex) {
            ex.getMessage();
        }
        return date;
    }

    /**
     * 计算当期时间减去N天之后的日期
     *
     * @param date 当期日期
     * @param day  N天
     * @return N天后的日期
     * @throws ParseException 异常
     */
    public static String dateDelDay(Date date, int day) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String dd = fmt.format(date);
        Date df = fmt.parse(dd);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(df);
        calendar.add(Calendar.DAY_OF_MONTH, 0 - day);
        String T = fmt.format(calendar.getTime());
        return T;
    }

    /**
     * Description:  计算当期时间减去N天之后的日期
     *
     * @param date
     * @param day
     * @return
     * @throws ParseException Date 2018年3月30日
     *                        Time 下午12:34:03
     */
    public static Date dateDelDay2date(Date date, int day) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        String dd = fmt.format(date);
        Date df = fmt.parse(dd);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(df);
        calendar.add(Calendar.DAY_OF_MONTH, 0 - day);
        return calendar.getTime();
    }

    /**
     * 计算当期时间减去N小时之后的日期
     *
     * @param date 当期日期
     * @param hour N小时
     * @return N小时后的日期
     * @throws ParseException 异常
     */
    public static String dateDelHour(Date date, int hour) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dd = fmt.format(date);
        Date df = fmt.parse(dd);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(df);
        calendar.add(Calendar.HOUR_OF_DAY, 0 - hour);
        String T = fmt.format(calendar.getTime());
        return T;
    }

    /**
     * Description: 获取某段时间内的所有日期
     * Date 2018年3月30日
     * Time 上午11:36:36
     */
    public static List<String> findDay2List(Date dBegin, Date dEnd) {
        List<String> lDate = new ArrayList<String>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        lDate.add(dateFormat.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (dEnd.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(dateFormat.format(calBegin.getTime()));
        }
        if (lDate.size() > 1) {
            lDate.remove(lDate.size() - 1);
        }
        return lDate;
    }

    /**
     * 计算两个日期之间的所有天数
     * Description:
     *
     * @param dBegin
     * @param dEnd
     * @return Date 2018年3月30日
     * Time 下午12:50:10
     */
    public static JSONArray findDay2Array(Date dBegin, Date dEnd) {
        JSONArray day = new JSONArray();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        day.add(Integer.valueOf(dateFormat.format(dBegin).split("-")[2]));
        Calendar calBegin = Calendar.getInstance();
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        calEnd.setTime(dEnd);
        while (dEnd.after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            day.add(Integer.valueOf(dateFormat.format(calBegin.getTime()).split("-")[2]));
        }
        if (day.size() > 1) {
            day.remove(day.size() - 1);
        }
        return day;
    }

    /**
     * 计算当期时间减去N小时之后的日期
     *
     * @param date 当期日期
     * @param hour N小时
     * @return N小时后的日期
     * @throws ParseException 异常
     */
    public static String dateSubHour(Date date, int hour) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date df = fmt.parse(fmt.format(date));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(df);
        calendar.add(Calendar.HOUR_OF_DAY, 0 - hour);
        String T = fmt.format(calendar.getTime());
        return T;
    }

    /**
     * 计算当期时间减去N分钟之后的日期
     *
     * @param date   当期日期
     * @param minute N分钟
     * @return N小时后的日期
     * @throws ParseException 异常
     */
    public static String dateSubMinute(Date date, int minute) throws ParseException {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(fmt.parse(fmt.format(date)));
        calendar.add(Calendar.MINUTE, 0 - minute);
        return fmt.format(calendar.getTime());
    }

    /**
     * 指定日期的开始时间（00:00:00）
     *
     * @param date
     * @return
     */
    public static Date getAppointDateStart(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
            cal.set(Calendar.HOUR_OF_DAY, 00);
            cal.set(Calendar.MINUTE, 00);
            cal.set(Calendar.SECOND, 00);
            cal.set(Calendar.MILLISECOND, 0);
        }
        return cal.getTime();
    }

    public static String getAppointDateStartStr(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        String dateStart = df.format(cal.getTime()) + date_start;
        return dateStart;
    }

    /**
     * 指定日期的结束时间（23:59:59）
     *
     * @param date
     * @return
     */
    public static Date getAppointDateEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
            cal.set(Calendar.HOUR_OF_DAY, 23);
            cal.set(Calendar.MINUTE, 59);
            cal.set(Calendar.SECOND, 59);
            cal.set(Calendar.MILLISECOND, 0);
        }
        return cal.getTime();
    }

    public static String getAppointDateEndStr(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        String dateStart = df.format(cal.getTime()) + date_end;
        return dateStart;
    }

    /**
     * @description: 指定日期所在月的第一天
     * @author yxq
     * @date 2025/5/24
     * @version 1.0
     */
    public static Date getAppointMonthStartDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Calendar cal_1 = Calendar.getInstance();
        cal_1.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), 1, 0, 0, 0);
        return cal_1.getTime();
    }

    /**
     * @description: 指定日期所在月的最后一天
     * @author yxq
     * @date 2025/5/24
     * @version 1.0
     */
    public static Date getAppointMonthLastDate(Date date) {
        Calendar cal_1 = Calendar.getInstance();
        cal_1.setTime(date);
        cal_1.set(Calendar.DAY_OF_MONTH, cal_1.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal_1.set(Calendar.HOUR, 23);
        cal_1.set(Calendar.MINUTE, 59);
        cal_1.set(Calendar.SECOND, 59);
        return cal_1.getTime();
    }

    /**
     * @description: 指定日期所在年的第一天
     * @author yxq
     * @date 2025/5/24
     * @version 1.0
     */
    public static String getAppointYearStartDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Calendar cal_1 = Calendar.getInstance();
        cal_1.set(cal.get(Calendar.YEAR), 0, 1);
        String yearStartDate = format(cal_1.getTime(), DEFAULT_DATE_FORMAT_YMD) + date_start;
        return yearStartDate;
    }

    /**
     * @description: 指定日期所在年的最后一天
     * @author yxq
     * @date 2025/5/24
     * @version 1.0
     */
    public static String getAppointYearLastDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        Calendar cal_1 = Calendar.getInstance();
        cal_1.set(cal.get(Calendar.YEAR), 11, 31);
        String yearEndDate = format(cal_1.getTime(), DEFAULT_DATE_FORMAT_YMD) + date_end;
        return yearEndDate;
    }

    /**
     * 获取两个日期之间的所有日期
     *
     * @param start
     * @param end
     * @return 返回Date类型的集合
     */
    public static List<Date> getBetweenDates(Date start, Date end) {
        List<Date> result = new ArrayList<Date>();
        Calendar tempStart = Calendar.getInstance();
        result.add(start);
        tempStart.setTime(start);
        tempStart.add(Calendar.DAY_OF_YEAR, 1);

        Calendar tempEnd = Calendar.getInstance();
        tempEnd.setTime(end);
        while (tempStart.before(tempEnd)) {
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_YEAR, 1);
        }
        result.add(end);
        return result;
    }

    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime
     * @param endTime
     * @return 返回String类型的集合
     */
    public static List<String> getDays(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        try {
            Date start = parseDate(startTime);
            Date end = parseDate(endTime);
            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * @description: 指定的日期内重写小时和分钟
     * @author yxq
     * @date 2025/5/24
     * @version 1.0
     */
    public static Date setAppointDateByHourAndMinute(Date date, int hour, int minute) {
        Calendar cal_1 = Calendar.getInstance();
        cal_1.setTime(date);
        cal_1.set(Calendar.DAY_OF_MONTH, cal_1.getActualMaximum(Calendar.DAY_OF_MONTH));
        cal_1.set(Calendar.HOUR, hour);
        cal_1.set(Calendar.MINUTE, minute);
        cal_1.set(Calendar.SECOND, 0);
        return cal_1.getTime();
    }


    public static void main(String[] args) {
        //System.out.println(getAppointYearStartDate(parseStringToDate("2020-02-19"))+"====="+getAppointYearLastDate(parseStringToDate("2020-02-19")));
        Date a = parseDate("2021-11-11");
        Date b = parseDate("2021-06-30");
        try {
            //System.out.println(getBetweenDates(a,b));
            //System.out.println(getDays(format(a),format(b)));
//            Date startDate = DateHelper.parseDate("05-08 06:00","MM-dd HH:mm");
//            System.out.println(startDate);

            int compare = DateHelper.compareToNow(DEFAULT_DATE_FORMAT_YMD, "2025-08-17");
            System.out.println(DateHelper.compareToNow(DEFAULT_DATE_FORMAT_YMD, "2025-08-16"));
            System.out.println(DateHelper.compareToNow(DEFAULT_DATE_FORMAT_YMD, "2025-08-17"));
            System.out.println(DateHelper.compareToNow(DEFAULT_DATE_FORMAT_YMD, "2025-08-18"));
        } catch (Exception e) {

        }
    }
}
