/**
 *
 */
package com.slodon.b2b2c.core.util;

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

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public final class DateUtil {
    //日志
    protected static Logger log = LoggerFactory.getLogger(DateUtil.class);

    /** yyyy-MM-dd. */
    public static final String DATE_FORMAT_ONE = "yyyy-MM-dd";

    /** yyyy/MM/dd. */
    public static final String DATE_FORMAT_TWO = "yyyy/MM/dd";

    /** yyyyMMdd. */
    public static final String DATE_FORMAT_THREE = "yyyyMMdd";

    /** yyyyMM. */
    public static final String DATE_FORMAT_FOUR = "yyyyMM";

    /** yyyy-MM-dd HH:mm:ss. */
    public static final String DATE_FORMAT_FIVE = "yyyy-MM-dd HH:mm:ss";

    /** yyyy-MM-dd HH:mm: */
    public static final String DATE_FORMAT_FIVEFIVE = "yyyy-MM-dd HH:mm";

    /** yyyyMMddHHmm */
    public static final String DATE_FORMAT = "yyyyMMddHHmmss";

    /** yyyy-MM-dd HH:mm:ss. */
    public static final String DATE_FORMAT_UPDATE = "yyyy-MM-dd HH:mm:ss.SSS";

    /** yyyy. */
    public static final String DATE_FORMAT_SIX = "yyyy";

    /** MM. */
    public static final String DATE_FORMAT_SEVEN = "MM";

    /** dd. */
    public static final String DATE_FORMAT_DD = "dd";

    /** yyyy/MM/dd HH:mm:ss. */
    public static final String DATE_FORMAT_EIGHT = "yyyy/MM/dd HH:mm:ss";

    /** HH:mm:ss. */
    public static final String DATE_FORMAT_NINE = "HH:mm:ss";

    /** yy/MM/dd. */
    public static final String DATE_FORMAT_TEN = "yy/MM/dd";

    /** yyyy/M/d. */
    public static final String DATE_FORMAT_ELEVEN = "yyyy/M/d";

    /** yyyy/MM. */
    public static final String DATE_FORMAT_TWELEVE = "yyyy/MM";

    /** yyyyMMddHHmmssSSS. */
    public static final String DATE_FORMAT_THIRTEEN = "yyyyMMddHHmmssSSS";

    /** yyMMdd. */
    public static final String DATE_FORMAT_FOURTEEN = "yyMMdd";

    /** yyyy.MM.dd. */
    public static final String DATE_FORMAT_SIXTEEN = "yyyy.MM.dd";

    /** yyyyMMddHHmmss. */
    public static final String DATE_FORMAT_SEVENTEEN = "yyyyMMddHHmmss";

    /** yyyy年MM月dd日 */
    public static final String DATE_FORMAT_EIGHTEEN = "yyyy年MM月dd日";

    /** HH:mm. */
    public static final String DATE_FORMAT_NINETEEN = "HH:mm";

    /** HHmm. */
    public static final String DATE_FORMAT_TWENTY = "HHmm";

    /** yyyy-MM */
    public static final String DATE_FORMAT_TWENTY_ONE = "yyyy-MM";

    /** yyyy.MM */
    public static final String DATE_FORMAT_TWENTY_TWO = "yyyy.MM";

    /** yyyy年MM月dd日HH:mm */
    public static final String DATE_FORMAT_FIFTEEN = "yyyy年MM月dd日 HH:mm";

    /** yyyy-MM-dd HH:mm */
    public static final String DATE_FORMAT_MMDDHHMM = "MM.dd HH:mm";

    /**
     * MM-dd
     */
    public static final String DATE_FORMAT_MMDD = "MM-dd";

    /** MMM d'th', yyyy EEE'.' */
    public static final String DATE_FORMAT_THIRTY = "EEE', 'MMM d'th', yyyy'.'";

    /** yyyy'年'MM'月'dd日， EEE */
    public static final String DATE_FORMAT_FORTY = "yyyy'年'MM'月'dd'日 'EEE";

    /** yyyy'年'MM'月'*/
    public static final String DATE_FORMAT_ZH_YM = "yyyy'年'MM'月'";

    /** yyyy-MM-dd */
    public static final String DATE_FORMAT_SIMPLE = "yyyy-MM-dd";

    /** MM月dd日 */
    public static final String DATE_FORMAT_MD = "MM月dd日";

    /** yyyy-MM-dd HH:mm */
    public static final String DATE_FORMAT_FIFTY = "yyyy-MM-dd HH:mm";

    /** dd号 HH:mm */
    public static final String DATE_FORMAT_DD_HH_MM = "dd号 HH:mm";


    /** INT_4. */
    private static final int INT_4 = 4;

    /** INT_6. */
    private static final int INT_6 = 6;

    /** INT_8. */
    private static final int INT_8 = 8;

    /** . */
    public static final String YEAR_FORMAT = "[12][0-9]{3}";

    /** 年. */
    public static final int YEAR = 1;

    /** 月. */
    public static final int MONTH = 2;

    /** 日. */
    public static final int DAY = 3;

    /**
     * 一天的毫秒数
     */
    public static final long DAY_MILLISECONDS = 24 * 60 * 60 * 1000;

    /**
     * 取得系统当前日期
     */
    public static Date getDate() {
        return new Date();
    }

    /**
     * 取得系统当前时间
     */
    public static long getTime() {
        return getDate().getTime();
    }

    /**
     * 取得系统当前日期
     */
    public static String getNowDate() {
        return dateToString(new Date(), DATE_FORMAT_ONE);
    }

    public static Date nowDate() {
        return formatDate(new Date(), DATE_FORMAT_ONE);
    }

    /**
     * 取得系统当前时间
     */
    public static String getNowDatetime() {
        return dateToString(new Date(), DATE_FORMAT_FIVE);
    }

    /**
     * 取得系统具体时间
     */
    public static String getDetailDate() {
        return dateToString(new Date(), DATE_FORMAT_FIFTEEN);
    }

    /**
     * 取得系统当前时间
     */
    public static String getNowTime() {
        return dateToString(new Date(), DATE_FORMAT_NINETEEN);
    }

    /**
     * 取得当前年份
     */
    public static String getThisYear() {
        return dateToString(new Date(), DATE_FORMAT_SIX);
    }

    /**
     * 获取当前的日期所在的星期
     * @param date 日期
     * @return 数字星期
     */
    public static Integer getCurrDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            dayOfWeek = 7;
        } else {
            dayOfWeek = dayOfWeek - 1;
        }
        return dayOfWeek;
    }

    /**
     * 按指定格式转化字符串为Date
     *
     * @param strDate String
     * @param strFormat String
     *
     * @return FormatDate
     *
     * @throws ParseException ParseException
     */
    public static Date getFormatDate(String strDate, String strFormat) throws ParseException {
        if (StringUtil.isEmpty(strDate) || StringUtil.isEmpty(strFormat)) {
            return null;
        }

        SimpleDateFormat df = new SimpleDateFormat(strFormat);
        df.setLenient(false);
        return df.parse(strDate);
    }

    /**
     * 按指定格式转化Date为字符串
     *
     * @param date Date
     * @param toFormat String
     *
     * @return FormatDate String
     */
    public static String dateToString(Date date, String toFormat) {

        if (date == null) {
            return "";
        }

        SimpleDateFormat df = new SimpleDateFormat(toFormat);
        df.setLenient(false);
        return df.format(date);
    }


    /**
     * 按指定格式转化Date为字符串
     *
     * @param date Date
     * @param strFormat String
     *
     * @return FormatDate String
     */
    public static Date formatDate(Date date, String strFormat) {
        if (date == null) {
            return null;
        }
        Date reDate = null;
        try {
            reDate = getFormatDate(dateToString(date, strFormat), strFormat);
        } catch (ParseException e) {
            date = null;
        }
        return reDate;
    }

    /**
     * <PRE>
     *
     * 比较两个日期
     *
     * </PRE>
     *
     * @param day1 String
     * @param day2 String
     * @return int day1>day2返回1，day1<day2返回-1，day1=day2返回0
     */
    public static int compareDate(String day1, String day2) {

        int isEarly = -1;

        if (day1 == null || day1.trim().equals("") || day2 == null || day2.trim

                ().equals("")) {
            isEarly = -1;
        } else {

            day1 = day1.replaceAll("-", "");
            day2 = day2.replaceAll("-", "");

            int year1 = Integer.parseInt(day1.substring(0, INT_4));
            int month1 = Integer.parseInt(day1.substring(INT_4, INT_6)) - 1;
            int day = Integer.parseInt(day1.substring(INT_6, INT_8));
            Calendar cal1 = Calendar.getInstance();
            cal1.set(year1, month1, day);

            int year2 = Integer.parseInt(day2.substring(0, INT_4));
            int month2 = Integer.parseInt(day2.substring(INT_4, INT_6)) - 1;
            int day22 = Integer.parseInt(day2.substring(INT_6, INT_8));
            Calendar cal2 = Calendar.getInstance();
            cal2.set(year2, month2, day22);

            if (cal1.getTime().compareTo(cal2.getTime()) > 0) {
                isEarly = 1;
            } else if (cal1.getTime().compareTo(cal2.getTime()) == 0) {
                isEarly = 0;
            }
        }
        return isEarly;
    }

    /**
     * <PRE>
     *
     * 比较两个日期
     *
     * </PRE>
     *
     * @param day1 String
     * @param day2 String
     * @param strFormat String
     * @throws ParseException 异常
     * @return int day1>day2返回1，day1<day2返回-1，day1=day2返回0
     */
    public static int compareDate(String day1, String day2, String strFormat) throws ParseException {
        Date date1 = getFormatDate(day1, DATE_FORMAT_THREE);
        Date date2 = getFormatDate(day2, DATE_FORMAT_THREE);
        return date1.compareTo(date2);
    }

    /**
     * 比较两个日期的大小
     *
     * @param date1
     * @param date2
     * @return int date1>date2返回1，date1<date2返回-1，date1=date2返回0
     */
    public static int compareDate(Date date1, Date date2) {
        return date1.compareTo(date2);
    }

    /**
     * 日期追加的计算
     *
     * @param strDate
     *            原日期(YYYYMMDD)
     * @param nAddNum
     *            追加的年月日的大小
     * @param nType
     *            追加的类型
     *
     * @return 追加后的新日期(YYYYMMDD)
     */
    public static String addDate(String strDate, int nAddNum, int nType) {
        int nYear = Integer.parseInt(strDate.substring(0, 4));
        int nMonth = Integer.parseInt(strDate.substring(4, 6)) - 1;
        int nDay = Integer.parseInt(strDate.substring(6));

        GregorianCalendar objCal = new GregorianCalendar();
        objCal.set(nYear, nMonth, nDay);

        switch (nType) {
            case 1: {
                objCal.add(GregorianCalendar.YEAR, nAddNum);
                break;
            }
            case 2: {
                objCal.add(GregorianCalendar.MONTH, nAddNum);
                break;
            }
            case 3: {
                objCal.add(GregorianCalendar.DATE, nAddNum);
                break;
            }
            case 4: {
                objCal.add(GregorianCalendar.HOUR, nAddNum);
                break;
            }
            case 5: {
                objCal.add(GregorianCalendar.MINUTE, nAddNum);
                break;
            }
            default: {
                break;
            }
        }

        return dateToString(objCal.getTime(), DATE_FORMAT_THREE);
    }

    /**
     * 日期追加的计算
     *
     * @param date
     *            原日期
     * @param nAddNum
     *            追加的年月日的大小
     * @param nType
     *            追加的类型
     *
     * @return 追加后的新日期
     */
    public static Date addDate(Date date, int nAddNum, int nType) {

        GregorianCalendar objCal = new GregorianCalendar();
        objCal.setTime(date);

        switch (nType) {
            case 1: {
                objCal.add(GregorianCalendar.YEAR, nAddNum);
                break;
            }
            case 2: {
                objCal.add(GregorianCalendar.MONTH, nAddNum);
                break;
            }
            case 3: {
                objCal.add(GregorianCalendar.DATE, nAddNum);
                break;
            }
            case 4: {
                objCal.add(GregorianCalendar.HOUR, nAddNum);
                break;
            }
            case 5: {
                objCal.add(GregorianCalendar.MINUTE, nAddNum);
                break;
            }
        }

        return objCal.getTime();
    }

    /**
     * 返回日期比较的中文格式
     *
     * @param date1 日期一
     * @param date2 日期二
     * @return ××年××月××天
     */
    public static String toChineseDiffDate(String date1, String date2) {

        int diffMonth = 0;

        int diffDay = 0;

        int year = Integer.parseInt(date1.substring(0, 4));
        int year1 = Integer.parseInt(date2.substring(0, 4));

        int month = Integer.parseInt(date1.substring(4, 6)) - 1;
        int month1 = Integer.parseInt(date2.substring(4, 6)) - 1;

        int day = Integer.parseInt(date1.substring(6, 8));
        int day1 = Integer.parseInt(date2.substring(6, 8));

        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(year, month, day, 0, 0, 0);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(year1, month1, day1, 0, 0, 0);

        Date enddate = endCalendar.getTime();
        Date startdate = startCalendar.getTime();
        int monthDays = startCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        while (startdate.before(enddate)) {
            diffDay++;
            startCalendar.add(Calendar.DATE, 1);
            startdate = startCalendar.getTime();
            if (diffDay == monthDays) {
                diffMonth++;
                diffDay = 0;
                monthDays = startCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            }
        }
        String strdate = "";
        if (diffMonth / 12 > 0) {
            strdate = strdate + diffMonth / 12 + "年";
        }
        if (diffMonth % 12 > 0) {
            strdate = strdate + diffMonth % 12 + "月";
        }
        if (strdate.equals("") || diffDay > 0) {
            strdate = strdate + diffDay + "日";
        }
        return strdate;
    }


    /**
     *
     * @param date 日期
     * @param strLangFlag 语言标志
     * @return 中文化的星期X
     */
    public static String getCnEnDayOfWeek(Date date, String strLangFlag) {
        String strDayOfWeek = "星期";
        String strEnDayOfWeek = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 2) {
            strDayOfWeek += "一";
            strEnDayOfWeek += "Monday";
        } else if (dayOfWeek == 3) {
            strDayOfWeek += "二";
            strEnDayOfWeek += "Tuesday";
        } else if (dayOfWeek == 4) {
            strDayOfWeek += "三";
            strEnDayOfWeek += "Wednesday";
        } else if (dayOfWeek == 5) {
            strDayOfWeek += "四";
            strEnDayOfWeek += "Thursday";
        } else if (dayOfWeek == 6) {
            strDayOfWeek += "五";
            strEnDayOfWeek += "Friday";
        } else if (dayOfWeek == 7) {
            strDayOfWeek += "六";
            strEnDayOfWeek += "Saturday";
        } else {
            strDayOfWeek += "日";
            strEnDayOfWeek += "Sunday";
        }
        if (StringUtil.isEmpty(strLangFlag)) {
            return strDayOfWeek;
        }
        return strEnDayOfWeek;

    }

    /**
     *
     * @param date 日期
     * @return 中文化的星期X
     * @throws ParseException
     */
    public static String getCnEnDayOfWeek(String date) throws ParseException {
        String strDayOfWeek = "星期";
        String strEnDayOfWeek = "";
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(stringToDate(date, DATE_FORMAT_ONE));
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 2) {
            strDayOfWeek += "一";
            strEnDayOfWeek += "Monday";
        } else if (dayOfWeek == 3) {
            strDayOfWeek += "二";
            strEnDayOfWeek += "Tuesday";
        } else if (dayOfWeek == 4) {
            strDayOfWeek += "三";
            strEnDayOfWeek += "Wednesday";
        } else if (dayOfWeek == 5) {
            strDayOfWeek += "四";
            strEnDayOfWeek += "Thursday";
        } else if (dayOfWeek == 6) {
            strDayOfWeek += "五";
            strEnDayOfWeek += "Friday";
        } else if (dayOfWeek == 7) {
            strDayOfWeek += "六";
            strEnDayOfWeek += "Saturday";
        } else {
            strDayOfWeek += "日";
            strEnDayOfWeek += "Sunday";
        }

        return strDayOfWeek;

    }

    /**
     *
     * @param date 日期
     * @param strLangFlag 日期
     * @return 英文日期
     */
    public static String getCurrentDayOfWeek(Date date, String strLangFlag) {
        SimpleDateFormat sdf = null;

        if (StringUtil.isEmpty(strLangFlag)) {
            sdf = new SimpleDateFormat(DATE_FORMAT_FORTY, Locale.CHINESE);
        } else {
            sdf = new SimpleDateFormat(DATE_FORMAT_THIRTY, Locale.ENGLISH);
        }
        String strDate = sdf.format(Calendar.getInstance().getTime());
        return strDate;

    }

    /**
     *
     * @param date 日期
     * @throws ParseException 异常
     * @return 今年最后一天 yyyy-MM-dd
     */
    public static Date getNowYearLastDay(Date date) throws ParseException {
        String lastDay = new SimpleDateFormat(DATE_FORMAT_SIX).format(date) + "-12-31";
        return new SimpleDateFormat(DATE_FORMAT_ONE).parse(lastDay);
    }

    /**
     *
     * @param date 日期
     * @throws ParseException 异常
     * @return 今年的第一天 yyyy-MM-dd
     */
    public static Date getNowYearFirstDay(Date date) throws ParseException {
        String firstDay = new SimpleDateFormat(DATE_FORMAT_SIX).format(date) + "-01-01";
        return new SimpleDateFormat(DATE_FORMAT_ONE).parse(firstDay);
    }

    /**
     *
     * @param sd1 日期
     * @param ed2 日期
     * @return 日期相隔天数
     */
    public static int diffDay(Date sd1, Date ed2) {
        if (null == sd1 || null == ed2) {
            return -1;
        }
        Calendar d1 = Calendar.getInstance();
        Calendar d2 = Calendar.getInstance();
        d1.setFirstDayOfWeek(Calendar.SUNDAY);
        d2.setFirstDayOfWeek(Calendar.SUNDAY);
        d1.setTime(sd1);
        d2.setTime(ed2);
        if (d1.after(d2)) { // swap dates so that d1 is start and d2 is end
            Calendar swap;
            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;
    }

    /**
     *
     * @param date 日期
     * @param date1 日期
     * @param n 倍数
     * @return 两个日期之间间隔天数是否是n的倍数
     */
    public static boolean isDayMultN(Date date, Date date1, int n) {
        return (diffDay(date, date1) % n) == 0;
    }

    /**
     *
     * @param date 日期
     * @param date1 日期
     * @return 日期相隔周数
     */
    public static int diffWeek(Date date, Date date1) {
        return (int) ((getNextMonday(date).getTime() - getNextMonday(date1).getTime()) / (1000 * 60 * 60 * 3600 * 7));
    }

    /**
     *
     * @param date 日期
     * @param date1 日期
     * @param n 倍数
     * @return 两个日期之间间隔周数是否是n的倍数
     */
    public static boolean isWeekMultN(Date date, Date date1, int n) {
        return (diffWeek(date, date1) % n) == 0;
    }

    /**
     *
     * @param date 日期
     * @return 获得周一的日期
     */
    public static Date getMonday(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return c.getTime();
    }

    /**
     *
     * @param date 日期
     * @return 获得周一的日期
     */
    public static Date getSunday(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        return c.getTime();
    }

    /**
     *
     * @param date 日期
     * @return 获得周六的日期
     */
    public static Date getSaturday(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        return c.getTime();
    }

    /**
     *
     * @param date 日期
     * @return 获得某日为该周第几天
     */
    public static int getDayOfWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * @param date 日期
     * @return 获得当月第一天
     */
    public static Date getFirstDayOfCurrMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.SUNDAY);
        calendar.setTime(date);
        int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.add(Calendar.DATE, 1 - dayOfMonth);
        return calendar.getTime();
    }

    public static String getLastDayOfMonth(int year,int month) {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }

    /**
     * 取得某天是一年中的多少周
     *
     * @param date date
     * @return 某天是一年中的多少周
     */
    public static int getWeekOfYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int currMonth = c.get(Calendar.MONTH);
        if (currMonth == 11 && c.get(Calendar.WEEK_OF_YEAR) == 1) {
            // 取上周周一
            c.add(Calendar.DATE, -7);
            return c.get(Calendar.WEEK_OF_YEAR) + 1;

        } else {
            return c.get(Calendar.WEEK_OF_YEAR);
        }
    }

    /**
     * 得到某一年周的总数
     *
     * @param year year
     * @return 某一年周的总数
     */
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        return getWeekOfYear(c.getTime());
    }

    /**
     *
     * @param date 日期
     * @return 该月第几周
     */
    public static int getWeekOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        c.setTime(date);
        return c.get(Calendar.WEEK_OF_MONTH);
    }

    /**
     * 获得日期的下一个星期一的日期
     *
     * @param day day
     * @return 日期的下一个星期一的日期
     */
    public static Date getNextMonday(Date day) {
        Calendar date = Calendar.getInstance();
        date.setFirstDayOfWeek(Calendar.SUNDAY);
        date.setTime(day);
        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.getTime();
    }

    /**
     * 获得本周第一天的日期(星期天为第一天)
     *
     * @param day day
     * @return 本周第一天的日期(星期天为第一天)
     */
    public static Date getFirstDayOfWeek(Date day) {
        Calendar date = Calendar.getInstance();
        date.setFirstDayOfWeek(Calendar.SUNDAY);
        date.setTime(day);
        date.set(Calendar.DAY_OF_WEEK, date.getFirstDayOfWeek());
        return date.getTime();
    }

    /**
     * 获得本周最后一天的日期(星期天为第一天)
     *
     * @param day day
     * @return 获得本周最后一天的日期(星期天为第一天)
     */
    public static Date getLastDayOfWeek(Date day) {
        Date firstDayOfWeek = getFirstDayOfWeek(day);
        Date lastDayOfWeek = addDay2(firstDayOfWeek, 6);
        return lastDayOfWeek;
    }

    /**
     * 获得日期包含月份的最大日期
     *
     * @param dateString 日期字符串 "2000-01-01"
     * @param applyPattern 日期的模式 "yyyy-MM-dd"
     * @throws ParseException 异常
     * @return 最大日期
     */
    public static int getMaxDayOfMonth(String dateString, String applyPattern) throws ParseException {
        int d = 0;

        Calendar c = Calendar.getInstance();
        c.setFirstDayOfWeek(Calendar.SUNDAY);
        SimpleDateFormat sdf = new SimpleDateFormat();
        sdf.applyPattern("yyyy-MM-dd");
        c.setTime(sdf.parse(dateString));
        d = c.getActualMaximum(Calendar.DAY_OF_MONTH);

        return d;
    }

    /**
     * 获取指定日期月份的第一天
     * @param date 日期
     * @return 字符串日期
     */
    public static String getFirstDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(c.getTime());
        return dateStr;
    }

    /**
     * 获取指定日期月份的第一天
     * @param date 日期
     * @return 字符串日期
     */
    public static Date getFirstDayOfMonth2(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, 1);
        return formatDate(c.getTime(), DATE_FORMAT_ONE);
    }

    /**
     * 获取指定日期月份的最后一天
     * @param date 日期
     * @return 字符串日期
     */
    public static Date getLastDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 1);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.add(Calendar.DAY_OF_MONTH, -1);
        return formatDate(c.getTime(), DATE_FORMAT_ONE);
    }

    /**
     * 在当前日期上追加N天
     * @param date 当前日期
     * @param num 添加天数
     * @return 日期字符串形式
     */
    public static String addDay(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, num);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = sdf.format(c.getTime());
        return dateStr;
    }

    /**
     * 在当前日期上追加N天
     * @param date 当前日期
     * @param num 添加天数
     * @return 日期字符串形式 yyyy-MM-dd HH:mm:ss
     */
    public static String addDayy(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, num);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(c.getTime());
        return dateStr;
    }

    /**
     * 在当前日期上追加N天
     * @param date 当前日期
     * @param num 添加天数
     * @return 日期字符串形式
     * @throws ParseException
     */
    public static Date addDay(String date, int num) throws ParseException {
        Date temDate = string2Date(date, null);
        Calendar c = Calendar.getInstance();
        c.setTime(temDate);
        c.add(Calendar.DAY_OF_MONTH, num);
        return c.getTime();
    }

    /**
     * 在当前日期上追加N天
     * @param date 当前日期
     * @param num 添加天数
     * @param format 日期格式
     * @return 日期字符串形式
     * @throws ParseException
     */
    public static String addDay(String date, int num, String format) throws ParseException {
        Date temDate = string2Date(date, null);
        Calendar c = Calendar.getInstance();
        c.setTime(temDate);
        c.add(Calendar.DAY_OF_MONTH, num);
        return DateUtil.dateToString(c.getTime(), format);
    }

    /**
     * 在当前日期上追加N月
     * @param date 当前日期
     * @param num 添加月数
     * @return Date
     * @throws ParseException
     */
    public static Date addMonth(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, num);
        return c.getTime();
    }

    /**
     * 在当前日期上追加N天
     * @param date 当前日期
     * @param num 添加天数
     * @return 日期字符串形式
     */
    public static Date addDay2(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, num);
        return c.getTime();
    }

    /**
     * 在当前日期上追加N天
     * @param date 当前日期
     * @param num 添加天数
     * @return 日期字符串形式
     */
    public static Date addMinute(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, num);
        return c.getTime();
    }

    /**
     * 在当前日期上追加N周
     * @param date 当前日期
     * @param num 添加周数
     * @return 日期字符串形式
     */
    public static Date addWeekDate(String date, int num) {
        Date weekDay = null;
        try {
            Date d1 = DateUtil.string2Date(date, DateUtil.DATE_FORMAT_ONE);
            weekDay = addWeek(d1, num);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return weekDay;
    }
    /**
     * 在当前日期上追加N周
     * @param date 当前日期
     * @param num 添加周数
     * @return 日期字符串形式
     */
    public static String addWeek(String date, int num) {
        String weekDay = "";
        try {
            Date d1 = DateUtil.string2Date(date, DateUtil.DATE_FORMAT_ONE);
            Date d2 = addWeek(d1, num);
            weekDay = DateUtil.dateToString(d2, DateUtil.DATE_FORMAT_ONE);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return weekDay;
    }

    /**
     * 在当前日期上追加N周
     * @param date 当前日期
     * @param num 添加周数
     * @return 日期字符串形式
     */
    public static Date addWeek(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.WEEK_OF_YEAR, num);
        return c.getTime();
    }

    /**
     * 在当前日期上追加N年
     * @param date 当前日期
     * @param num 添加年数
     * @return 日期字符串形式
     */
    public static Date addYear(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, num);
        return c.getTime();
    }

    /**
     * 根据指定的格式转换字符串为日期，格式为空默认使用“yyyy-MM-dd”
     * @param date 日期字符串
     * @param format 格式
     * @return Date
     * @throws ParseException
     */
    public static Date string2Date(String date, String format) throws ParseException {
        if (StringUtil.isEmpty(format)) {
            format = DATE_FORMAT_ONE;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }

    /**
     * 根据指定的格式转换字符串为日期，格式为空默认使用“yyyy-MM-dd”
     * @param date 日期字符串
     * @param format 格式
     * @return Date
     * @throws ParseException
     */
    public static Date stringToDate(String date, String format) throws ParseException {
        if (StringUtil.isEmpty(format)) {
            format = DATE_FORMAT_ONE;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(date);
    }

    /**
     * 获取当前时间指定星期名所在的日期
     * @param weekDayName 星期名称，值为Calendar.DAY_OF_WEEK
     */
    public static Date getDayWeek(int weekDayName) {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_WEEK, Calendar.FRIDAY);
        Date dayWeek = c.getTime();
        return dayWeek;
    }

    /**
     * 获取当前时间所在月份第一天
     */
    public static Date getFirstDayOfMonth() {
        Calendar c = Calendar.getInstance();
        c.set(Calendar.DAY_OF_MONTH, 1);
        return c.getTime();
    }

    /**
     * 获取当前时间所在月份
     */
    public static Integer getCurrMonth() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前时间所在年份
     */
    public static Integer getCurrYear() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR);
    }

    /**
     * 获取当前时间所在月份
     */
    public static Integer getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取当前时间所在年份
     */
    public static Integer getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.YEAR);
    }

    /**
     * 获取当前时间所在日
     */
    public static Integer getDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DATE);
    }


    /**
     * 获得本周周一
     *
     * @param day day
     * @return 本周周一
     */
    public static Date getMondayOfWeek(Date day) {
        Calendar date = Calendar.getInstance();
        date.setFirstDayOfWeek(Calendar.MONDAY);
        date.setTime(day);
        date.set(Calendar.DAY_OF_WEEK, date.getFirstDayOfWeek());
        return date.getTime();
    }

    /**
     * 获取当前时间字符串表示的小时数
     * @param date 时间字符串
     * @return Integer 当前时间小时数
     */
    public static Integer getMinute(Date date) {
        Integer minute = null;
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            minute = c.get(Calendar.MINUTE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return minute;
    }


    /**
     * 在当前日期上追加N小时
     * @param date 当前日期
     * @param num 添加小时数
     * @return 日期字符串形式
     */
    public static Date addHour(Date date, int num) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.HOUR_OF_DAY, num);
        return c.getTime();
    }

    /**
     * 指定的时间戳获取日期对象
     * @param milliseconds 时间戳
     * @return Date
     */
    public static Date getDate(Long milliseconds) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(milliseconds);
        return c.getTime();
    }

    /**
     * 计算两个日期之间相隔的天数，如果d2在d1之后返回正数，d2在d1之前返回负数，返回0表示相等
     * @param d1
     * @param d2
     */
    public static long compareDiffDay(Date d1, Date d2) {
        long days = 0;
        if (d1 != null && d2 != null) {
            long diffMillisecounds = d2.getTime() - d1.getTime();
            days = diffMillisecounds / DAY_MILLISECONDS;
            long moreNum = diffMillisecounds % DAY_MILLISECONDS;
            if (moreNum > 0) {
                days = days + 1;
            } else if (moreNum < 0) {
                days = days - 1;
            }
        }
        return days;
    }

    /**
     * 获取当前时间字符串表示的小时数
     * @param date 时间字符串
     * @param parent 时间格式
     * @return Integer 当前时间小时数
     */
    public static Integer getHour(String date, String parent) {
        Integer hour = null;
        Date tempDate;
        try {
            tempDate = stringToDate(date, parent);
            Calendar c = Calendar.getInstance();
            c.setTime(tempDate);
            hour = c.get(Calendar.HOUR_OF_DAY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hour;
    }

    /**
     * 获取当前时间字符串表示的小时数
     * @param date 时间字符串
     * @return Integer 当前时间小时数
     */
    public static Integer getHour(Date date) {
        Integer hour = null;
        try {
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            hour = c.get(Calendar.HOUR_OF_DAY);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hour;
    }

    /**
     * 获取当前时间字符串表示的分钟数
     * @param day 时间字符串
     * @return Integer 当前时间分钟数
     */
    public static String getMinute(String day) {
        String str = "";

        day = day.replaceAll("-", "");

        int min = Integer.parseInt(day.substring(12, 14));

        if (min <= 9) {
            str = "0" + String.valueOf(min);
        } else {
            str = String.valueOf(min);
        }

        return str;
    }

    /**
     * 获取当前时间字符串表示的小时数
     * @param day 时间格式
     * @return Integer 当前时间小时数
     */
    public static int getHour(String day) {

        day = day.replaceAll("-", "");

        int hour = Integer.parseInt(day.substring(9, 11));

        return hour;
    }

    /**
     * 获取在当月中的天数
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 以友好的方式显示时间
     *
     * @param datetime
     * @return
     */
    public static String friendlyTime(Date datetime) {
        if (datetime == null) {
            return "";
        }
        String ftime = "";
        long currTime = DateUtil.getTime();
        long paramTime = datetime.getTime();
        // 判断是否是同一天
        String curDate = DateUtil.dateToString(new Date(), DateUtil.DATE_FORMAT_ONE);
        String paramDate = DateUtil.dateToString(datetime, DateUtil.DATE_FORMAT_ONE);
        if (curDate.equals(paramDate)) {
            int hour = (int) ((currTime - paramTime) / 3600000);
            if (hour == 0) {
                ftime = Math.max((currTime - paramTime) / 60000, 1) + "分钟前";
            } else {
                ftime = hour + "小时前";
            }
            return ftime;
        }
        long lt = paramTime / 86400000;
        long ct = currTime / 86400000;
        int days = (int) (ct - lt);
        if (days == 0) {
            int hour = (int) ((paramTime - currTime) / 3600000);
            if (hour == 0) {
                ftime = Math.max((paramTime - currTime) / 60000, 1) + "分钟前";
            } else {
                ftime = hour + "小时前";
            }
        } else if (days == 1) {
            ftime = "昨天";
        } else if (days == 2) {
            ftime = "前天";
        } else if (days > 2 && days <= 10) {
            ftime = days + "天前";
        } else if (days > 10) {
            ftime = DateUtil.dateToString(datetime, DateUtil.DATE_FORMAT_ONE);
        }
        return ftime;
    }

    /**
     * 获取当日零点
     * @param date
     * @return
     */
    public static Date getTodayZeroTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取当日零点
     * @param date
     * @return
     */
    public static Date getTodayZeroTime(String date) throws ParseException {
        Date dateTime = stringToDate(date, DATE_FORMAT_ONE);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateTime);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取明日零点
     * @param date
     * @return
     */
    public static Date getTomZeroTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 获取明日零点
     * @param date
     * @return
     */
    public static Date getTomZeroTime(String date) throws ParseException {
        return getTomZeroTime(DateUtil.getFormatDate(date, DateUtil.DATE_FORMAT_ONE));
    }

    /**
     * 计算2个日期相差多少分钟
     * @param d1 较大时间
     * @param d2 较小时间
     * @return
     */
    public static int getCompareDiffDayMin(Date d1, Date d2) {
        long diffMillisecounds = d1.getTime() - d2.getTime();
        return (int) (diffMillisecounds / (1000 * 60));
    }

    public static String getCountDown(long currentTime, long targetTime) {
        long distTime = targetTime - currentTime;
        if (distTime <= 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer("");
        long day = ((distTime / 1000) / (3600 * 24));
        long hour = ((distTime / 1000) - day * 86400) / 3600;
        long minutes = ((distTime / 1000) - day * 86400 - hour * 3600) / 60;
        long seconds = (distTime / 1000) - day * 86400 - hour * 3600 - minutes * 60;
        if (day > 0) {
            sb.append(day).append(" 天  ");
        }

        if (hour < 10) {
            sb.append("0").append(hour);
        } else {
            sb.append(hour);
        }
        sb.append(":");

        if (minutes < 10) {
            sb.append("0").append(minutes);
        } else {
            sb.append(minutes);
        }
        sb.append(":");

        if (seconds < 10) {
            sb.append("0").append(seconds);
        } else {
            sb.append(seconds);
        }

        return sb.toString();

    }

    public static String getCountDown(long currentTime, long targetTime, String expireStr) {

        String countDownStr = getCountDown(currentTime, targetTime);
        if (StringUtil.isEmpty(countDownStr)) {
            countDownStr = expireStr;
        }
        return countDownStr;

    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = format.format(today);
        return result;
    }

    /**
     * 获取两个日期之间的日期
     * @param start 开始日期
     * @param end 结束日期
     * @return 日期集合
     */
    public static List<String> getBetweenDates(String start, String end) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> result = new ArrayList<String>();
        int diffdays = diffDay(format.parse(start), format.parse(end));
        if (diffdays >= 0) {
            for (int i = 0; i <= diffdays; i++) {
                String date = addDay(format.parse(start), i);
                result.add(date);
            }
        }

        return result;
    }

    /**
     * 获取两个日期之间的日期
     * @param start 开始日期
     * @param end 结束日期
     * @return 日期集合
     */
    public static List<String> getBetweenDates(Date start, Date end) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        List<String> result = new ArrayList<String>();
        int diffdays = diffDay(start, end);
        if (diffdays >= 0) {
            for (int i = 0; i <= diffdays; i++) {
                String date = addDay(start, i);
                result.add(date);
            }
        }

        return result;
    }


    /**
     * 获取当前季度
     *
     */
    public static int getQuarter() {
        Calendar c = Calendar.getInstance();
        int month = c.get(c.MONTH) + 1;
        int quarter = 0;
        if (month >= 1 && month <= 3) {
            quarter = 1;
        } else if (month >= 4 && month <= 6) {
            quarter = 2;
        } else if (month >= 7 && month <= 9) {
            quarter = 3;
        } else {
            quarter = 4;
        }
        return quarter;
    }

    /**
     * 获取某季度的第一天和最后一天
     * @param num 第几季度
     * @return
     */
    public static String[] getCurrQuarter(int num) {
        String[] s = new String[2];
        String str = "";
        // 设置本年的季
        Calendar quarterCalendar = null;
        switch (num) {
            case 1: // 本年到现在经过了一个季度，在加上前4个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 3);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateUtil.dateToString(quarterCalendar.getTime(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "01-01";
                s[1] = str;
                break;
            case 2: // 本年到现在经过了二个季度，在加上前三个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 6);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateUtil.dateToString(quarterCalendar.getTime(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "04-01";
                s[1] = str;
                break;
            case 3:// 本年到现在经过了三个季度，在加上前二个季度
                quarterCalendar = Calendar.getInstance();
                quarterCalendar.set(Calendar.MONTH, 9);
                quarterCalendar.set(Calendar.DATE, 1);
                quarterCalendar.add(Calendar.DATE, -1);
                str = DateUtil.dateToString(quarterCalendar.getTime(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "07-01";
                s[1] = str;
                break;
            case 4:// 本年到现在经过了四个季度，在加上前一个季度
                quarterCalendar = Calendar.getInstance();
                str = DateUtil.dateToString(quarterCalendar.getTime(), "yyyy-MM-dd");
                s[0] = str.substring(0, str.length() - 5) + "10-01";
                s[1] = str.substring(0, str.length() - 5) + "12-31";
                break;
        }
        return s;
    }

    /**
     *
     * @param timeMillis 时间戳
     * @param format 格式
     * @author 汤星鹏
     * @Description 时间戳转换
     * @return java.lang.String
     * @date 2019/12/23 12:59
     */
    public static String timeMillisToDate(Long timeMillis, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String sd = sdf.format(new Date(Long.parseLong(String.valueOf(timeMillis))));
        return sd;
    }

    /**
     *
     * @param date
     * @author 汤星鹏
     * @Description 判断该日期是不是今天
     * @return boolean
     * @date 2020/3/4 14:13
     */
    public static boolean isThisToday(Date date) {
        Date now = getDate();
        Date start = getTodayZeroTime(now);
        Date end = getTomZeroTime(now);
        boolean rs = false;
        int large = compareDate(start, date);
        int small = compareDate(end, date);
        if (large <= 0 && small == 1) {
            return true;
        }
        return rs;
    }


    /**
     *
     * @param date
     * @author 汤星鹏
     * @Description 判断是不是昨天
     * @return boolean
     * @date 2020/3/6 17:35
     */
    public static boolean isYesterday(Date date) {
        Date now = getDate();
        Date start = DateUtil.addDay2(getTodayZeroTime(now), -1);
        Date end = DateUtil.addDay2(getTomZeroTime(now), -1);
        boolean rs = false;
        int large = compareDate(start, date);
        int small = compareDate(end, date);
        if (large <= 0 && small == 1) {
            return true;
        }
        return rs;
    }

    /**
     *
     * @param date
     * @author 汤星鹏
     * @Description 判断是不是本周
     * @return boolean
     * @date 2020/3/6 17:35
     */
    public static boolean isThisWeek(Date date) {
        Date now = getDate();
        Date start = DateUtil.getFirstDayOfWeek(now);
        Date end = DateUtil.getTomZeroTime(DateUtil.getLastDayOfMonth(now));
        boolean rs = false;
        int large = compareDate(start, date);
        int small = compareDate(end, date);
        if (large <= 0 && small == 1) {
            return true;
        }
        return rs;
    }

    /**
     *
     * @param sd1 日期(yyyy-MM-dd)
     * @param ed2 日期(yyyy-MM-dd)
     * @author 汤星鹏
     * @Description 日期相隔天数, 包括其本身。如:03-01 至 03-02 为2天
     * @date 2020/3/11 8:03
     */
    public static int diffDay(String sd1, String ed2) throws ParseException {
        Date start = DateUtil.stringToDate(sd1, DateUtil.DATE_FORMAT_SIMPLE);
        Date end = DateUtil.stringToDate(ed2, DateUtil.DATE_FORMAT_SIMPLE);
        return diffDay(start, end);
    }

    /**
     *
     * @param sd1 日期(yyyy-MM-dd)
     * @param ed2 日期(yyyy-MM-dd)
     * @author 汤星鹏
     * @Description 日期相隔天数, 包括其本身。如:03-01 至 03-02 为2天
     * @date 2020/3/11 8:03
     */
    public static int diffDayInclude(String sd1, String ed2) throws ParseException {
        Date start = DateUtil.stringToDate(sd1, DateUtil.DATE_FORMAT_SIMPLE);
        Date end = DateUtil.stringToDate(ed2, DateUtil.DATE_FORMAT_SIMPLE);
        return diffDay(start, end) + 1;
    }

    /**
     *
     * @param sd1 日期(yyyy-MM-dd)
     * @param ed2 日期(yyyy-MM-dd)
     * @author 汤星鹏
     * @Description 日期相隔天数, 包括其本身。如:03-01 至 03-02 为2天
     * @date 2020/3/11 8:03
     */
    public static int diffDayInclude(Date sd1, Date ed2) throws ParseException {
        return diffDay(sd1, ed2) + 1;
    }

    private static Map weekNumberMap = new HashMap();

    static {
        weekNumberMap.put(0, 1);
        weekNumberMap.put(1, 2);
        weekNumberMap.put(2, 3);
        weekNumberMap.put(3, 4);
        weekNumberMap.put(4, 5);
        weekNumberMap.put(5, 6);
        weekNumberMap.put(6, 7);


    }

    public static Object getWeekNum(int strWeek) {
        return weekNumberMap.get(strWeek);
    }

    public static String weekForNum(List weekDays) {
        // 返回结果为组合的星期系数
        String weekNumber = "";
        for (Object weekDay : weekDays) {
            weekNumber = weekNumber + "" + getWeekNum((Integer) weekDay).toString();
        }
        return weekNumber;


    }

    public static Integer dayForWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    public static List getDates(String dateFrom, String dateEnd, List weekDays) {
        long time;
        long perDayMilSec = 24L * 60 * 60 * 1000;
        List dateList = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        // 需要查询的星期系数
        String strWeekNumber = weekForNum(weekDays);
        try {
            dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
            while (true) {
                time = sdf.parse(dateFrom).getTime();
                time = time + perDayMilSec;
                Date date = new Date(time);
                dateFrom = sdf.format(date);
                if (dateFrom.compareTo(dateEnd) <= 0) {
                    // 查询的某一时间的星期系数
                    Integer weekDay = dayForWeek(date);
                    // 判断当期日期的星期系数是否是需要查询的
                    if (strWeekNumber.contains(weekDay.toString())) {
                        dateList.add(dateFrom);
                    }
                } else {
                    break;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateList;
    }

    public static List<String> dateParse(String startData, String endData) {
        List<String> resultList = new ArrayList<>();

        if (StringUtil.isEmpty(startData) || StringUtil.isEmpty(endData)) {
            return resultList;
        }

        SimpleDateFormat sdf;
        if (startData.length() < 10) {
            sdf = new SimpleDateFormat("yyyy-MM");
            Date d1 = null;//定义起始日期
            Date d2 = null;//定义结束日期
            try {
                d1 = sdf.parse(startData);
                d2 = sdf.parse(endData);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Calendar dd = Calendar.getInstance();//定义日期实例
            dd.setTime(d1);//设置日期起始时间
            while (dd.getTime().before(d2)) {//判断是否到结束日期
                String str = sdf.format(dd.getTime());
                System.out.println(str);
                resultList.add(str);
                dd.add(Calendar.MONTH, 1);//进行当前日期月份加1
            }
            resultList.add(endData);
        } else {
            sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dBegin = null;
            Date dEnd = null;
            try {
                dBegin = sdf.parse(startData);
                dEnd = sdf.parse(endData);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            List<Date> lDate = new ArrayList();
            lDate.add(dBegin);
            Calendar calBegin = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calBegin.setTime(dBegin);
            Calendar calEnd = Calendar.getInstance();
            // 使用给定的 Date 设置此 Calendar 的时间
            calEnd.setTime(dEnd);
            // 测试此日期是否在指定日期之后
            while (dEnd.after(calBegin.getTime())) {   // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
                calBegin.add(Calendar.DAY_OF_MONTH, 1);
                lDate.add(calBegin.getTime());
            }
            for (Date date : lDate) {
//                System.out.println(sdf.format(date));
                resultList.add(sdf.format(date));
            }
        }

        return resultList;
    }

    public static int daysBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(between_days));
    }
    public static List<String> getPaymentDateArray(String qdrq, String jsrq, int i, String mcfkr) throws ParseException {
        List<String> resultList = new ArrayList<>();

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        //获取起点时间 结束时间
        Date qdrqDate = sf.parse(qdrq);
        Date jsrqDate = sf.parse(jsrq);
        if (qdrq.substring(qdrq.length() - 2, qdrq.length()).equals("01")) {
            String tempStr = sf.format(qdrqDate);
            resultList.add(tempStr);
        }

        Date tempDate = addMonths(qdrqDate, i);

        while (tempDate.getTime() < jsrqDate.getTime()) {
            String tempStr = sf.format(tempDate);
            tempStr = getDesignateDate(tempDate, tempStr, mcfkr);
            resultList.add(tempStr);
            tempDate = addMonths(tempDate, i);


        }
        if (jsrq.substring(jsrq.length() - 2, jsrq.length()).equals("01")) {
            String tempStr = sf.format(jsrqDate);
            resultList.add(tempStr);

        }
        return resultList;

    }

    /**
     * 根据传入的数据进行日期的拼接
     *
     * @param tempDate 日期对象
     * @param tempStr  传入的日期字符串 格式 2019-05-03
     * @param mcfkr    传入的一月的哪一天 为数字 需要补零
     * @return
     */
    public static String getDesignateDate(Date tempDate, String tempStr, String mcfkr) {
        //如果传入的默认几日为空 直接返回传入的日期字符串
        if (StringUtil.isEmpty(mcfkr)) {
            return tempStr;
        }
        Integer tempInt = Integer.parseInt(mcfkr);
        //如果默认日 大于28 则进行判断 如果30号  二月份应该给28 有30天的月份应该给30
//		if (tempInt >= 28) {
//			Calendar cal = Calendar.getInstance();
//			cal.setTime(tempDate);
//			int actualMaximum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//			if (actualMaximum < tempInt) {
//				tempInt = actualMaximum;
//			}
//		}
        String resultStr = tempStr.substring(0, tempStr.length() - 2) + (tempInt > 10 ? tempInt + "" : "0" + tempInt);
        return resultStr;
    }

    /**
     * 给指定日期添加 i 月
     *
     * @param targetDate
     * @param i          添加的月数
     * @return
     */
    public static Date addMonths(Date targetDate, int i) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(targetDate);
        cal.add(Calendar.MONTH, i);
        return cal.getTime();
    }

    /**
     *
     * @param second
     * @return
     */
    public static String secondToFormat(int second){
        int h = second / 3600;
        int m = second % 3600 / 60;
        int s = second % 60; //不足60的就是秒，够60就是分
        return String.format("%02d",h) + ":" + String.format("%02d",m) + ":" + String.format("%02d",s);
    }

    public static void main(String[] args) {
        System.out.println(secondToFormat(12001));
    }

    public static int getYearOfQuarter(int month) {
        Calendar c = Calendar.getInstance();
        int quarter = 0;
        if (month >= 1 && month <= 3) {
            quarter = 1;
        } else if (month >= 4 && month <= 6) {
            quarter = 2;
        } else if (month >= 7 && month <= 9) {
            quarter = 3;
        } else {
            quarter = 4;
        }
        return quarter;
    }

    public static String getLastYear(Date date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        return format.format(y);
    }


}
