package com.zcxk.core.utils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

/**
 * @author Andy
 * @description: 日期工具类
 * @date 2021/7/16
 * @version V1.0
 **/
public class DateUtil extends DateFormatUtils {

    private static Pattern pattern = Pattern.compile(
            "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s([0-1][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$");

    private static String[] parsePatterns = {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss",
        "yyyy-MM-dd HH:mm", "yyyy-MM",
        "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd",
        "yyyy.MM.dd HH:mm:ss",
        "yyyy.MM.dd HH:mm", "yyyy.MM", "yyyyMMddHHmmss"};

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String YYYYMMDDHHMMSS = "yyyy-MM-dd HH:mm:ss";
    /**
     * yyyy-MM-dd
     */
    public static final String YYYYMMDD = "yyyy-MM-dd";
    /**
     * yyyy-MM
     */
    public static final String YYYYMM = "yyyy-MM";

    /**
     * EEEE
     */
    public static final String WEEK = "EEEE";

    public static final String[] WEEKS = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    public static final String[] WEEKS_SIMPLE = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd）
     */
    public static String getDate() {
        return getDate("yyyy-MM-dd");
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd HH:mm）
     */
    public static String getCurrentDate() {
        return getDate(parsePatterns[1]);
    }

    /**
     * 得到当前日期字符串 格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String getDate(String pattern) {
        return format(new Date(), pattern);
    }

    public static String getSeqString() {
        return format(new Date(), "yyyyMMddHHmmss");
    }


    /**
     * 得到日期字符串 默认格式（yyyy-MM-dd） pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static String formatDate(Date date, Object... pattern) {
        if (null != date) {
            if (pattern != null && pattern.length > 0) {
                return format(date, pattern[0].toString());
            } else {
                return format(date, "yyyy-MM-dd");
            }
        }
        return null;
    }

    /**
     * 得到Date pattern可以为："yyyy-MM-dd" "HH:mm:ss" "E"
     */
    public static Date parseDate(String strDate, Object... pattern) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        try {
            Date date;
            SimpleDateFormat sdf;
            if (pattern != null && pattern.length > 0) {
                sdf = new SimpleDateFormat(pattern[0].toString());
            } else {
                sdf = new SimpleDateFormat("yyyy-MM-dd");
            }
            date = sdf.parse(strDate);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取时间戳，格式“20180420113136502”
     */
    public static String getTimeStamp() {
        return format(new Date(), "yyyyMMddHHmmssSSS");
    }

    /**
     * 得到日期时间字符串，转换格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String formatDateTime(Date date) {
        return formatDate(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到Date，转换格式（yyyy-MM-dd HH:mm:ss）
     */
    public static Date parseDateTime(String dateStr) {
        return parseDate(dateStr, "yyyy-MM-dd HH:mm:ss");
    }


    /**
     * 得到当前时间字符串 格式（HH:mm:ss）
     */
    public static String getTime() {
        return formatDate(new Date(), "HH:mm:ss");
    }

    /**
     * 得到当前日期整数 格式（yyyyMMdd）
     */
    public static Integer getDate(Date date) {
        return Integer.valueOf(formatDate(date, "yyyyMMdd"));
    }

    /**
     * 得到当前日期和时间字符串 格式（yyyy-MM-dd HH:mm:ss）
     */
    public static String getDateTime() {
        return formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 得到当前年份字符串 格式（yyyy）
     */
    public static String getYear() {
        return formatDate(new Date(), "yyyy");
    }

    /**
     * 得到当前年份整数 格式（yyyy）
     */
    public static Integer getYear(Date date) {
        return Integer.valueOf(formatDate(date, "yyyy"));
    }

    /**
     * 得到当前月份字符串 格式（MM）
     */
    public static String getMonth() {
        return formatDate(new Date(), "MM");
    }

    /**
     * 得到当天字符串 格式（dd）
     */
    public static String getDay() {
        return formatDate(new Date(), "dd");
    }

    /**
     * 得到当前星期字符串 格式（E）星期几
     */
    public static String getWeek() {
        return formatDate(new Date(), "E");
    }

    /**
     * 获取过去的天数
     */
    public static long pastDays(Date date) {
        long t = System.currentTimeMillis() - date.getTime();
        return t / (24 * 60 * 60 * 1000);
    }

    /**
     * 获取过去的小时
     */
    public static long pastHour(Date date) {
        long t = System.currentTimeMillis() - date.getTime();
        return t / (60 * 60 * 1000);
    }

    /**
     * 获取过去的分钟
     */
    public static long pastMinutes(Date date) {
        long t = System.currentTimeMillis() - date.getTime();
        return t / (60 * 1000);
    }

    /**
     * 转换为时间（天,时:分:秒.毫秒）
     */
    public static String formatDateTime(long timeMillis) {
        long day = timeMillis / (24 * 60 * 60 * 1000);
        long hour = (timeMillis / (60 * 60 * 1000) - day * 24);
        long min = ((timeMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
        long s = (timeMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        long sss = (timeMillis - day * 24 * 60 * 60 * 1000 - hour * 60 * 60 * 1000 - min * 60 * 1000
            - s * 1000);
        return (day > 0 ? day + "," : "") + hour + ":" + min + ":" + s + "." + sss;
    }

    /**
     * 获取两个日期之间的天数
     */
    public static double getDistanceOfTwoDate(Date before, Date after) {
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return (afterTime - beforeTime) / (1000 * 60 * 60 * 24);
    }

    /**
     * 获取两个日期之间的天数
     */
    public static int differentDaysByMillisecond(Date after, Date before) {
        int days = (int) ((after.getTime() - before.getTime()) / (1000 * 3600 * 24));
        return days;
    }

    /**
     * 获取前 days 天
     *
     * @param days 间隔天数
     * @param pattern 返回格式
     */
    public static String getIntervalDate(int days, String pattern) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, days);
        return formatDate(calendar.getTime(), pattern);
    }

    /**
     * 根据String日期转String日期
     *
     * @return 取不到返回null
     */
    public static String getTimeByString(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }

        String year = str.substring(0, 4);
        String month = str.substring(4, 6);
        String day = str.substring(6, 8);
        String hour = str.substring(8, 10);
        String minute = str.substring(10, 12);
        String second = str.substring(12, 14);

        StringBuilder timeBuilder = new StringBuilder();
        timeBuilder.append(year).append("-");
        timeBuilder.append(month).append("-");
        timeBuilder.append(day).append(" ");
        timeBuilder.append(hour).append(":");
        timeBuilder.append(minute).append(":");
        timeBuilder.append(second);
        return timeBuilder.toString();
    }

    /**
     * 根据String日期转String 格式 yyyy-MM-dd
     *
     * @return 取不到返回null
     */
    public static String getYmdByString(String str) {
        String time = "";
        if (StringUtils.isEmpty(str)) {
            return time;
        }
        String year = str.substring(0, 4);
        String month = str.substring(4, 6);
        String day = str.substring(6, 8);
        time = year + "-" + month + "-" + day;

        return time;
    }

    /**
     * 获得本月第一天0点时间
     */
    public static Date getTimesMonthMorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0,
            0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        return cal.getTime();
    }

    /**
     * 获得本月最后一天24点时间
     */
    public static Date getTimesMonthNight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0,
            0, 0);
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH) - 1);
        cal.set(Calendar.HOUR_OF_DAY, 24);
        return cal.getTime();
    }

    /**
     * 获得当月的开始时间
     */
    public static String getStartDateSameMonth(String pattern) {
        Date startDate = getTimesMonthMorning();
        return formatDate(startDate, pattern);
    }

    /**
     * 获得当月最后一天的时间
     */
    public static String getEndDateSameMonth(String pattern) {
        Date endDate = new Date();
        return formatDate(endDate, pattern);
    }

    /**
     * 获取当前时间之前或者之后几个月 正数表示后 负数表示前
     */
    public static String getAfterMonth(int month) {
        // 获得一个日历的实例
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        // 设置日历时间
        c.setTime(date);
        // 在日历的月份上增加6个月
        c.add(Calendar.MONTH, month);
        // 的到你想要得6个月后的日期
        String strDate = sdf.format(c.getTime());
        return strDate;

    }

    /**
     * 将秒数转换为日时分秒，
     */
    public static String secondToTime(long second) {
        // 转换天数
        long days = second / 86400;
        // 剩余秒数
        second = second % 86400;
        // 转换小时
        long hours = second / 3600;
        // 剩余秒数
        second = second % 3600;
        // 转换分钟
        long minutes = second / 60;
        // 剩余秒数
        second = second % 60;
        if (days > 0) {
            return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
        } else {
            return hours + "小时" + minutes + "分" + second + "秒";
        }
    }

    /**
     * 将日期转换为日时分秒
     */
    public static String dateToTime(String date, String dateStyle) {
        if (StringUtils.isBlank(date)) {
            return null;
        }

        try {
            SimpleDateFormat format = new SimpleDateFormat(dateStyle);
            Date oldDate = format.parse(date);
            // 输入日期转换为毫秒数
            long time = oldDate.getTime();
            // 当前时间毫秒数
            long nowTime = System.currentTimeMillis();
            // 二者相差多少毫秒
            long second = nowTime - time;
            // 毫秒转换为妙
            second = second / 1000;
            long days = second / 86400;
            second = second % 86400;
            long hours = second / 3600;
            second = second % 3600;
            long minutes = second / 60;
            second = second % 60;
            if (days > 0) {
                return days + "天" + hours + "小时" + minutes + "分" + second + "秒";
            } else {
                return hours + "小时" + minutes + "分" + second + "秒";
            }
        } catch (ParseException e) {
        }
        return null;
    }

    /**
     * 格式化Timestamp时间
     *
     * @param timestamp Timestamp类型时间
     * @return 格式化后的字符串
     */
    public static String parseTimestampToStr(Timestamp timestamp, String timeFromat) {
        if (null == timestamp || StringUtils.isBlank(timeFromat)) {
            return null;
        }

        return format(timestamp, timeFromat);
    }

    /**
     * 获取当天开始时间
     */
    public static String dayStartDate() {
        return formatDate(new Date()) + " " + "00:00:00";
    }

    /**
     * 获取当天结束时间
     */
    public static String dayEndDate() {
        return formatDate(new Date()) + " " + "23:59:59";
    }

    /**
     * 根据时间获取时间戳
     */
    public static int parseStrToTimestamp(String time, String timeFromat) {
        try {
            if (StringUtils.isNotBlank(time) && StringUtils.isNotBlank(timeFromat)) {
                SimpleDateFormat sdf = new SimpleDateFormat(timeFromat);
                return Integer.parseInt(String.valueOf(sdf.parse(time).getTime() / 1000));
            }
        } catch (Exception e) {
        }
        return 0;
    }

    /**
     * 指定日期加上天数后的日期
     *
     * @param num 为增加的天数
     * @param newDate 创建时间
     */
    public static String plusDay(int num, String newDate) throws ParseException {
        if (StringUtils.isNotBlank(newDate)) {
            SimpleDateFormat format = new SimpleDateFormat(parsePatterns[1]);
            Date currdate = format.parse(newDate);
            Calendar ca = Calendar.getInstance();
            // num为增加的天数，可以改变的
            ca.add(Calendar.DATE, -num);
            currdate = ca.getTime();
            return format.format(currdate);
        }
        return null;
    }

    /**
     * 根据指定时间+间隔时间与当前时间比 大:true
     */
    public static Boolean beyoneIntervalTime(Date lastRefundTime, int minutes) {
        if (lastRefundTime == null) {
            return false;
        }

        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(new Date());
        Calendar lastRefundCal = Calendar.getInstance();
        lastRefundCal.setTime(lastRefundTime);
        lastRefundCal.add(Calendar.MINUTE, minutes);
        if (lastRefundCal.after(nowCal)) {
            return true;
        }
        return false;
    }

    /**
     * 时间加上Minute分钟
     */
    public static Date addMinute(Date date, int minute) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) + ((long) minute) * 60 * 1000);
        return c.getTime();
    }

    /**
     * 时间减去上Minute分钟
     */
    public static Date rmMinute(Date date, int minute) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) - ((long) minute) * 60 * 1000);
        return c.getTime();
    }

    /**
     * 时间减去多少秒
     */
    public static Date rmSecond(Date date, int second) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(getMillis(date) - second * 1000);
        return c.getTime();
    }

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

    /**
     * 日期  月份加减  （+，-）
     */
    public static String addOrRmMouth(Date date, int mouth) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, mouth);
        return DateUtil.getDateToStr(calendar.getTime(), false);
    }


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

    /**
     * 转换date为字符串类型
     */
    public static String dateToString(Date date, String format) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }

    /**
     * yyyy.MM.dd
     */
    public static String toYyyyMmDdf(Calendar date) {
        return format(date.getTime(), "yyyy.MM.dd");
    }

    /**
     * yyyy-MM-dd
     */
    public static String toYyyyMmDd(Calendar date) {
        return format(date.getTime(), "yyyy-MM-dd");
    }

    /**
     * 根据时间错得到DATE类
     */
    public static Date longToDate(long date) {
        return new Date(date);
    }

    /**
     * yyyyMMdd
     */
    public static String toYyyyMmDd2(Calendar date) {
        return format(date.getTime(), "yyyyMMdd");
    }

    /**
     * YYYYMMDDHHMMSS
     */
    public static String calToYyyyMmDdMmSs(Calendar date) {
        return format(date.getTime(), YYYYMMDDHHMMSS);
    }

    /**
     * yyyyMMdd
     */
    public static String toYyyyMmDd(Date date) {
        return format(date, "yyyyMMdd");
    }

    /**
     * MMdd
     */
    public static String toMmDd(Date date) {
        return format(date, "MMdd");
    }

    /**
     * M月d日
     */
    public static String toMmDd(Calendar date) {
        return format(date.getTime(), "M月d日");
    }

    /**
     * yyyy年MM月dd日
     */
    public static String toYyyyMmDdzh(Calendar date) {
        return format(date.getTime(), "yyyy年MM月dd日");
    }

    /**
     * yyMMddHH
     */
    public static String toyyMmDdHh(Calendar date) {
        return format(date.getTime(), "yyMMddHH");
    }

    /**
     * MM-dd HH:mm
     */
    public static String toMnDdHhMm(Date date) {
        return format(date, "MM-dd HH:mm");
    }

    /**
     * yyyyMMddHHmm
     */
    public static String toYyyyMmddHhmm(Date date) {
        return format(date, "yyyyMMddHHmm");
    }

    /**
     * yyyyMM
     */
    public static String toYyyyMm(Date date) {
        return format(date, "yyyyMM");
    }

    /**
     * YYYY-MM
     */
    public static String toYyyyMm2(Date date) {
        return format(date, YYYYMM);
    }

    /**
     * yyMM
     */
    public static String toyyMm(Date date) {
        return format(date, "yyMM");
    }

    /**
     * yyyyMMdd24Hmmss
     */
    public static String toYyyyMmdd24Hmmss(Date date) {
        return format(date, "yyyyMMdd24Hmmss");
    }

    /**
     * yyyyMMddHHmmss
     */
    public static String toYyyyMmddHmmss(Date date) {
        return format(date, "yyyyMMddHHmmss");
    }

    /**
     * HH:mm
     */
    public static String toHhmm(Date date) {
        return format(date, "HH:mm");
    }

    /**
     * HH:mm:ss
     */
    public static String toHhmmss(Date date) {
        return format(date, "HH:mm:ss");
    }

    /**
     * yyyy-MM-dd
     */
    public static String toYyyyyMmDd(Date date) {
        return format(date, "yyyy-MM-dd");
    }

    /**
     * yyyy-MM-dd HH:mm
     */
    public static String toYyyyMmDdHhMm(Date date) {
        return format(date, "yyyy-MM-dd HH:mm");
    }

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static String toYyyyMdHms(Date date) {
        return format(date, "yyyy-MM-dd HH:mm:ss");
    }

    public static Calendar getTheDayZero() {
        return getTheDayZero(DateUtil.now());
    }

    public static Calendar getTheDayLast() {
        return getTheDayLast(DateUtil.now());
    }

    public static Date stringToDateYyyyMmDdHhMmSs(String dateStr) {
        if (null == dateStr || "".equals(dateStr)) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            cal.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {

            return null;
        }
        return cal.getTime();
    }

    public static Date stringToDateFmt(String dateStr, String fmt) {
        if (null == dateStr || "".equals(dateStr)) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        try {
            cal.setTime(sdf.parse(dateStr));
        } catch (ParseException e) {

            return null;
        }
        return cal.getTime();
    }

    public static Date parse(String dateStr, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(dateStr);
        } catch (Exception e) {
        }
        return null;
    }

    public static String toDateStamp(Calendar calendar) {
        return new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());
    }

    public static Calendar now() {
        return Calendar.getInstance();
    }

    /**
     * 返回此日期的零点整，如2007-3-14 19:00:35 返回 2007-3-14 00:00:00
     */
    public static Calendar getTheDayZero(Calendar date) {
        Calendar result = (Calendar) date.clone();
        result.set(Calendar.HOUR_OF_DAY, 0);
        result.set(Calendar.MINUTE, 0);
        result.set(Calendar.SECOND, 0);
        result.set(Calendar.MILLISECOND, 0);
        return result;
    }

    public static Calendar getTheMinuteZero(Calendar date) {
        Calendar result = (Calendar) date.clone();
        result.set(Calendar.MINUTE, 0);
        result.set(Calendar.SECOND, 0);
        result.set(Calendar.MILLISECOND, 0);
        return result;
    }

    /**
     * 返回此日期的最后的时单，如2007-3-14 19:00:35 返回 2007-3-14 23:59:59
     */
    public static Calendar getTheDayLast(Calendar date) {
        Calendar result = (Calendar) date.clone();
        result.set(Calendar.HOUR_OF_DAY, 23);
        result.set(Calendar.MINUTE, 29);
        result.set(Calendar.SECOND, 59);
        result.set(Calendar.MILLISECOND, 999);
        return result;
    }

    public static String getWeek(Date date) {
        String[] weekDays = {"周日", "周一", "周二", "周三", "周四", "周五", "周六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 两个日期是否是同一天
     */
    public static boolean areSameDay(Date dateA, Date dateB) {
        Calendar calDateA = Calendar.getInstance();
        calDateA.setTime(dateA);

        Calendar calDateB = Calendar.getInstance();
        calDateB.setTime(dateB);

        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
            && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
            && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
            .get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 两个日期是否是同一天
     */
    public static boolean areSameDay(Calendar calDateA, Calendar calDateB) {
        return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
            && calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
            && calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
            .get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获得2个日期的间隔天数，忽略时分秒.
     */
    public static int getDays(Calendar startDay, Calendar endDay) {
        if (startDay == null || endDay == null) {
            return 0;
        }

        long startMillis = startDay.getTimeInMillis();
        long endMillis = endDay.getTimeInMillis();
        long millis = endMillis - startMillis;

        int days = (int) (millis / (24 * 60 * 60 * 1000));

        return (days == 0 ? 1 : days);
    }

    /**
     * 获得一年中某周的第一天
     *
     * @create_time 2014年4月17日 下午4:54:38
     */
    public static Date getFirstDayOfWeek(int year, int weekOfYear) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        // 18为周数
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getActualMinimum(Calendar.DAY_OF_WEEK)
            - dayOfWeek);
        cal.add(Calendar.DATE, 1);
        return cal.getTime();
    }

    /**
     * 获得一年中某周的最后一天
     *
     * @create_time 2014年4月17日 下午4:59:14
     */
    public static Date getLastDayOfWeek(int year, int weekOfYear) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        // 18为周数
        cal.set(Calendar.WEEK_OF_YEAR, weekOfYear);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getActualMaximum(Calendar.DAY_OF_WEEK)
            - dayOfWeek);
        cal.add(Calendar.DATE, 1);
        return cal.getTime();
    }

    /**
     * 根据日期得到当月第几周
     */
    public static int getWeekOfTheMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_MONTH);
        return weekOfMonth;
    }

    /**
     * 根据日期得到当年第几周
     */
    public static int getWeekOfTheYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekOfMonth = calendar.get(Calendar.WEEK_OF_YEAR);
        return weekOfMonth;
    }

    /**
    * 获取当前时间所在年的周数
    * @author Andy
    * @date 14:36 2021/8/3
    * @param date:
    * @return int
    **/
    public static int getWeekOfYear(Date date) {
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
    * 获取当前时间所在年的最大周数
    * @author Andy
    * @date 14:36 2021/8/3
    * @param year:
    * @return int
    **/
    public static int getMaxWeekNumOfYear(int year) {
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);

        return getWeekOfYear(c.getTime());
    }

    /**
     * 获取当前周 日期 cycle 周期
     *
     * @return List
     */
    public static List<String> getWeekdays(String format) {
        int size = 7;
        Calendar calendar = Calendar.getInstance();
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DATE, -1);
        }
        List<String> weekList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            weekList.add(dateFormat.format(calendar.getTime()));
            calendar.add(Calendar.DATE, 1);
        }
        return weekList;
    }

    /**
     * 查看中文时间间隔 例如:5分钟前
     */
    public static String getTimeItervalDes(Date parmDate) {
        long interval = System.currentTimeMillis() - parmDate.getTime();
        int time = (int) (interval / (12 * 30 * 24 * 60 * 60 * 1000));
        if (time > 0) {
            return time + "年前";
        }
        time = (int) (interval / (30 * 24 * 60 * 60 * 1000));
        if (time > 0) {
            return time + "月前";
        }
        time = (int) (interval / (24 * 60 * 60 * 1000));
        if (time > 0) {
            return time + "天前";
        }
        time = (int) (interval / (60 * 60 * 1000));
        if (time > 0) {
            return time + "小时前";
        }
        time = (int) (interval / (60 * 1000));
        if (time > 0) {
            return time + "分钟前";
        }
        time = (int) (interval / 1000);
        if (time > 0) {
            return time + "秒前";
        }
        return "刚刚";

    }

    /**
     * 字符串转化为Date
     *
     * @return String 日期字符串
     */
    public static Date getStrToDate(String dateStr) {

        try {
            if (StringUtils.isBlank(dateStr)) {
                return null;
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat();
                return sdf.parse(dateStr);
            }
        } catch (ParseException e) {
            throw new RuntimeException("字符串转化为Date异常!");
        }
    }

    /**
     * 获取昨天的日期
     *
     * @param date 日期
     * @return String  yyyy-MM-dd
     */
    public static Date getNextDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }

    /**
     * Date转化为字符串
     *
     * @param date 日期
     * @return String  yyyy-MM-dd HH:mm:ss
     */
    public static String getDateToStr(Date date) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS);
            return sdf.format(date);
        }
    }

    /**
     * Date转化为字符串
     *
     * @param date 日期
     * @param format 格式
     * @return String
     */
    public static String getDateToStr(Date date, String format) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(date);
        }
    }

    /**
     * Date转化为字符串
     *
     * @param date 日期
     * @param b (true 包含当天  23:59:59，false不包含当天 00:00:00)
     * @return String
     */
    public static String getDateToStr(Date date, boolean b) {
        if (b) {
            return DateUtil.getDateToStr(date, DateUtil.YYYYMMDD) + " 23:59:59";
        } else {
            return DateUtil.getDateToStr(date, DateUtil.YYYYMMDD) + " 00:00:00";
        }
    }

    /**
     * 当前日期转化为字符串
     *
     * @param b (true 包含当天  23:59:59，false不包含当天 00:00:00)
     * @return String
     */
    public static String getCurrDateToStr(boolean b) {
        Date date = new Date();
        if (b) {
            return DateUtil.getDateToStr(date, DateUtil.YYYYMMDD) + " 23:59:59";
        } else {
            return DateUtil.getDateToStr(date, DateUtil.YYYYMMDD) + " 00:00:00";
        }
    }

    /**
     * 字符串转化为Date
     *
     * @return String 日期字符串
     */
    public static Date getStrToDate(String dateStr, String format) {

        try {
            if (StringUtils.isBlank(dateStr)) {
                return null;
            } else {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                return sdf.parse(dateStr);
            }
        } catch (ParseException e) {
            throw new RuntimeException("字符串转化为Date异常!");
        }
    }

    /**
     * 获取当前日期字符串
     *
     * @param format 格式(yyyyMMdd、yyyyMMddHHmmss)
     * @return String
     */
    public static long getCurDateToNum(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return Integer.parseInt(sdf.format(new Date()));
    }

    /**
     * 获取当前日期且转换为long
     *
     * @param format 格式如：(yyyyMMdd、yyyyMMddHHmmss)
     * @return String
     */
    public static long getDateToNum(Date date, String format) {
        return Integer.parseInt(DateUtil.getDateToStr(date, format));
    }


    public static long getDateNum(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return DateUtil.getDateToNum(sdf.format(date), format);
    }


    public static long getDateToNum(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date time = null;
        try {
            time = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time.getTime();
    }


    /**
     * 获取某天是周几字符串
     *
     *
     * 日期
     *
     * @return String  周一 ...
     */
    public static String getWeekSimple() {
        Calendar cal = Calendar.getInstance();
        int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex < 0) {
            weekIndex = 0;
        }
        return WEEKS_SIMPLE[weekIndex];
    }


    /**
     * 获取某天是星期几字符串
     *
     * @return String 星期一  ...
     */
    public static String getWeekXqSimple() {
        Calendar cal = Calendar.getInstance();
        int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex < 0) {
            weekIndex = 0;
        }
        return WEEKS[weekIndex];
    }


    public static int getWeekCount(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.getWeekYear();
    }

    /**
     * 获取某天是周几字符串
     *
     * @param date 日期
     * @return String
     */
    public static String getWeekSimple(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int weekIndex = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekIndex < 0) {
            weekIndex = 0;
        }
        return WEEKS_SIMPLE[weekIndex];
    }


    /**
     * 获取周几字符串
     *
     * @return int
     */
    public static int getWeekDayNo() {
        Calendar cd = Calendar.getInstance();
        int dayOfWeek = cd.get(7);
        if (dayOfWeek == 1) {
            return 7;
        } else {
            return dayOfWeek - 1;
        }
    }

    /**
     * 获取某一天
     *
     * @param days 天数
     * @return Date
     */
    public static Date add(int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }


    /**
     * 获取某时间加 减几天
     *
     * @param days 天数
     * @return Date
     */
    public static Date addOneDate(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        return calendar.getTime();
    }


    /**
     * 获取之前多少天集合(不包括当前天)
     *
     * @param days 天数
     * @return List<Date>
     */
    public static List<Date> getBefore(int days) {
        Calendar calendar = Calendar.getInstance();
        List<Date> list = new ArrayList<Date>();
        for (int i = 1; i <= days; i++) {
            calendar.add(5, -1);
            list.add(calendar.getTime());
        }
        Collections.reverse(list);
        return list;
    }

    /**
     * 获取之前多少天集合(包括当前天)
     *
     * @param days 天数
     * @return List<Date>
     */
    public static List<Date> getBefore1(int days) {
        Calendar calendar = Calendar.getInstance();
        List<Date> list = new ArrayList<Date>();
        list.add(calendar.getTime());
        for (int i = 1; i < days; i++) {
            calendar.add(5, -1);
            list.add(calendar.getTime());
        }
        Collections.reverse(list);
        return list;
    }

    /**
     * 获取之后多少天集合(不包括当前天)
     *
     * @param days 天数
     * @return List<Date>
     */
    public static List<Date> getAfter(int days) {
        Calendar calendar = Calendar.getInstance();
        List<Date> list = new ArrayList<Date>();
        for (int i = 1; i <= days; i++) {
            calendar.add(5, 1);
            list.add(calendar.getTime());
        }
        return list;
    }

    /**
     * 获取当前日期前后N天
     */
    public static List<String> getCurInterval(int days) {
        List<Date> list = new ArrayList<Date>();
        List<Date> beforeList = getBefore(days);
        List<Date> afterList = getAfter(days);

        list.addAll(beforeList);
        list.add(new Date());
        list.addAll(afterList);

        List<String> rlist = new ArrayList<String>();
        for (Date date : list) {
            rlist.add(getDateToStr(date, YYYYMMDD));
        }

        return rlist;
    }

    /**
     * 获取之后多少天集合(包括当前天)
     *
     * @param days 天数
     * @return List<Date>
     */
    public static List<Date> getAfter1(int days) {
        Calendar calendar = Calendar.getInstance();
        List<Date> list = new ArrayList<Date>();
        list.add(calendar.getTime());
        for (int i = 1; i < days; i++) {
            calendar.add(5, 1);
            list.add(calendar.getTime());
        }

        return list;
    }

    /**
     * 获取时间段日期
     *
     * @param startDate 起始日期
     * @param endDate 结束日期
     * @return List<Date>
     */
    public static List<Date> getDates(Date startDate, Date endDate) {
        List<Date> list = new ArrayList<Date>();

        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        c.setTime(startDate);
        list.add(c.getTime());
        for (; c.getTime().before(endDate); list.add(c.getTime())) {
            c.add(5, 1);
        }

        return list;
    }

    /**
     * 获取当前周已过的日期
     *
     * @return List<Date>（起始+结束）
     */
    public static List<Date> getCalcThisWeek() {

        List<Date> list = new ArrayList<Date>();
        GregorianCalendar calendar = new GregorianCalendar();
        int minus = calendar.get(7) - 2;
        if (minus < 0) {
            return null;
        } else {
            list.add(calendar.getTime());
            calendar.add(5, -minus);
            list.add(calendar.getTime());

            return list;
        }
    }

    /**
     * 计算两日期之间天数
     *
     * @param startDate 起始日期
     * @param endDate 结束日期
     * @return int
     */
    public static int getDay(Date startDate, Date endDate) {
        if (startDate.after(endDate)) {
            Date cal = startDate;
            startDate = endDate;
            endDate = cal;
        }
        long sl = startDate.getTime();
        long el = endDate.getTime();
        long ei = el - sl;
        return (int) (ei / 0x5265c00L);
    }


    /**
     * 计算两日期之间天数
     *
     * @param startDate 起始日期
     * @param endDate 結束日期
     * @return int
     */
    public static int getDay(String startDate, String endDate)
        throws ParseException {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDD);

        Date s = sdf.parse(startDate);
        calendar.setTime(s);
        long timethis = calendar.getTimeInMillis();

        Date e = sdf.parse(endDate);
        calendar.setTime(e);
        long timeend = calendar.getTimeInMillis();

        return (int) (timeend - timethis) / (1000 * 60 * 60 * 24);
    }

    /**
     * 得到今天的日期时间数组串,前面小时间,后面大时间
     *
     * @return ['2014-03-29 00:00:00','2014-03-29 23:59:59']
     */
    public static String[] getNearTodayDateTimeStr() {
        Calendar cal = Calendar.getInstance();
        String today = getDateToStr(cal.getTime(), YYYYMMDD);
        return new String[]{today + " 00:00:00", today + " 23:59:59"};
    }

    public static Timestamp getCurTimesTamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * 判断当前时间与end时间的间隔有没有超过1分钟
     */
    public static long subTimeOfOneMinute(long end) {
        return subTimeOfOneMinute(System.currentTimeMillis(), end);
    }

    /**
     * 判断start与end时间的间隔有没有超过1分钟
     */
    public static long subTimeOfOneMinute(long start, long end) {
        return subTimeOfTime(start, end, 60);
    }

    /**
     * 判断当前时间与end时间的间隔有没有超过10分钟
     */
    public static long subTimeOfTenMinute(long end) {
        return subTimeOfTenMinute(System.currentTimeMillis(), end);
    }

    /**
     * 判断start与end时间的间隔有没有超过10分钟
     */
    public static long subTimeOfTenMinute(long start, long end) {
        return subTimeOfTime(start, end, 60 * 10);
    }

    /**
     * 判断start与end时间的间隔有没有超过time分钟
     */
    public static long subTimeOfTime(long start, long end, long time) {
        long t = start / 1000 - end / 1000;
        if (t < time) {
            return time - t;
        }
        return 0;
    }

    /**
     * 显示：多少分钟前，多少小时前，多少天前的效果，7天后扩展至1周前，1个月前，1年前
     */
    public static String showTimeBefore(Date date, String format) {
        String returnTime = "";
        if (date == null) {
            return returnTime;
        }
        if (format == null) {
            format = "yyyy-MM-dd HH:mm";
        }
        long nowtimeLong = System.currentTimeMillis();
        long dateLong = date.getTime();
        long result = Math.abs(nowtimeLong - dateLong);
        if (result < 60000) {
            long seconds = result / 1000;
            returnTime = seconds + "秒钟前";
        } else if (result >= 60000 && result < 3600000) {
            long seconds = result / 60000;
            returnTime = seconds + "分钟前";
        } else if (result >= 3600000 && result < 86400000) {
            long seconds = result / 3600000;
            returnTime = seconds + "小时前";
        } else if (result >= 86400000) {
            long seconds = result / 86400000;
            returnTime = seconds + "天前";
        } else {
            returnTime = new SimpleDateFormat(format).format(date);
        }
        return returnTime;
    }

    /**
     * 获取当天开始时间
     *
     * @return Date
     */
    public static Date getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取当天结束时间
     *
     * @return Date
     */
    public static Date getEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 24);
        todayEnd.set(Calendar.MINUTE, 0);
        todayEnd.set(Calendar.SECOND, 0);
        todayEnd.set(Calendar.MILLISECOND, 0);
        return todayEnd.getTime();
    }

    /**
     * 获取当月第一天
     */
    public static Calendar getFirstDayOfMonth(Calendar memberDate) {
        Calendar result = (Calendar) memberDate.clone();
        result.set(Calendar.DAY_OF_MONTH, 1);
        result.set(Calendar.HOUR_OF_DAY, 0);
        result.set(Calendar.MINUTE, 0);
        result.set(Calendar.SECOND, 1);
        return result;
    }

    /**
     * 获取当月最后一天
     */
    public static Calendar getlastDayOfMonth(Calendar memberDate) {
        Calendar result = (Calendar) memberDate.clone();
        result.set(Calendar.DAY_OF_MONTH, result.getActualMaximum(Calendar.DAY_OF_MONTH));
        result.set(Calendar.HOUR_OF_DAY, 0);
        result.set(Calendar.MINUTE, 0);
        result.set(Calendar.SECOND, 1);
        return result;
    }

    /**
     * 获取当天2015-01-23 23:59:59
     */
    public static Date getCurrentEndTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 23);
        todayEnd.set(Calendar.MINUTE, 59);
        todayEnd.set(Calendar.SECOND, 59);
        todayEnd.set(Calendar.MILLISECOND, 59);
        return todayEnd.getTime();
    }

    /**
     * 2015-01-23 00:00:01
     */
    public static Date getCurrentStartTime() {
        Calendar todayEnd = Calendar.getInstance();
        todayEnd.set(Calendar.HOUR_OF_DAY, 0);
        todayEnd.set(Calendar.MINUTE, 0);
        todayEnd.set(Calendar.SECOND, 1);
        todayEnd.set(Calendar.MILLISECOND, 1);
        return todayEnd.getTime();
    }


    public static Calendar getStrToCalendarDate(String dateStr, int type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calenTime = Calendar.getInstance();
        Date date = null;
        try {
            date = sdf.parse(dateStr);
            calenTime.setTime(date);
            if (type == 1) {
                calenTime = DateUtil.getTheDayZero(calenTime);
            } else if (type == 2) {
                calenTime = DateUtil.getTheDayLast(calenTime);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calenTime;
    }

    public static List<String> getmonthLastDayOfDate(Date sDate1, int removeMonth,
        String dateFormat) {
        List<String> dateList = new ArrayList<String>();
        for (int i = 1; i <= removeMonth; i++) {
            Calendar cDay1 = Calendar.getInstance();
            cDay1.setTime(sDate1);
            cDay1.set(Calendar.MONTH, cDay1.get(Calendar.MONTH) - i);
            int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
            cDay1.set(Calendar.DAY_OF_MONTH, lastDay);
            Date lastDate = cDay1.getTime();
            String dateStr = DateUtil.getDateToStr(lastDate, dateFormat);
            dateList.add(dateStr);
        }
        return dateList;
    }

    /**
     * getWeekDay:获取当前周的第一天和最后一天. <br/>
     *
     * @author liangxp
     * @since JDK 1.6
     */
    public static Map<String, String> getWeekDay() {
        Map<String, String> map = new HashMap<String, String>();
        Calendar cal = Calendar.getInstance();
        // 国外日期周日为第一天 故减去一天
        cal.add(Calendar.DAY_OF_YEAR, -1);
        // 获取本周一的日期
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        map.put("mon", DateUtil.getDateToStr(cal.getTime(), false));
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
        cal.add(Calendar.WEEK_OF_YEAR, 1);
        map.put("sun", DateUtil.getDateToStr(cal.getTime(), true));
        return map;
    }

    /**
     * getBeforeDay:获取前一天的时间. <br/>
     *
     * @author liangxp
     * @since JDK 1.6
     */
    public static Date getBeforeDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }

    /**
     * getCalendarBeforeDay:获取前一天的时间. <br/>
     *
     * @author liangxp
     * @since JDK 1.6
     */
    public static Calendar getCalendarBeforeDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar;
    }

    /**
     * getBeforeDay:获取后一天的时间. <br/>
     *
     * @author liangxp
     * @since JDK 1.6
     */
    public static Date getAfterDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        date = calendar.getTime();
        return date;
    }

    public static Date getAfterYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, 1);
        date = calendar.getTime();
        return date;
    }

    /**
     * getBeforeMonth:获取前一个月. <br/>
     *
     * @since JDK 1.6
     */
    public static Date getBeforeMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        return calendar.getTime();
    }

    /**
     * getBeforeMonth:获取前N月的日期. <br/>
     *
     * @author ljf
     * @since JDK 1.6
     */
    public static String getBeforeMonthDate(int n) {
        //获取当前时间
        Date da = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(da);
        //设置为前N月
        calendar.add(Calendar.MONTH, -n);
        //获取N个月前的时间
        da = calendar.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat(YYYYMMDDHHMMSS);
        return sdf.format(da);
    }


    /**
     * 根据时间戳获取 天时分秒
     */
    public static String getTimeBylongNum(long time) {
        String str = "";
        time = time / 1000;
        int tian = (int) (time / 3600 * 24);
        if (tian > 0) {
            str += tian + "天";
        }
        int shi = (int) (time / 3600);
        if (shi > 0) {
            str += shi + "时";
        }
        int fen = (int) (time / 60 % 60);
        if (fen > 0) {
            str += fen + "分";
        }
        int miao = (int) (time % 60);
        if (miao > 0) {
            str += miao + "秒";
        }
        return str;

    }

    /**
     * 根据时间戳获取秒
     */
    public static long getSecBylongNum(long time) {
        long miao = (long) time / 1000;
        return miao;

    }

    /**
     * 根据时间戳 生成日期String
     */
    public static String getDateStrForTimestamp(long timestamp, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String cTime = sdf.format(new Date(timestamp));
        return cTime;
    }

    /**
     * 根据日期String转换成时间戳
     */
    public static long getTimestampForDateStr(String dateStr, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date date = null;
        long timeStemp = 0L;
        try {
            date = sdf.parse(dateStr);
            timeStemp = date.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStemp;
    }

    /**
     * 返回时间差以秒为单位
     */
    public static long getdDifference(Calendar cal1, Calendar cal2) {
        return (cal2.getTimeInMillis() - cal1.getTimeInMillis()) * 1000;
    }

    public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
        List<Date> lDate = new ArrayList<>();
        // 把开始时间加入集合
        lDate.add(beginDate);
        Calendar cal = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间  
        cal.setTime(beginDate);
        boolean bContinue = true;
        while (bContinue) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量  
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后  
            if (endDate.after(cal.getTime())) {
                lDate.add(cal.getTime());
            } else {
                break;
            }
        }
        // 把结束时间加入集合
        lDate.add(endDate);
        return lDate;
    }

    public static boolean isLastDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1));
        if (calendar.get(Calendar.DAY_OF_MONTH) == 1) {
            return true;
        }
        return false;
    }


    /**
     * 获取前N个月的第一天DateTime
     */
    public static String getStartMinMonthDate(int n) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH, -n);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(instance.getTime()) + " 00:00:00";
        return format1;
    }

    /**
     * 获取前N个月的第一天Date
     */
    public static String getStartMinMonthDateD(int n) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH, -n);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(instance.getTime());
        return format1;
    }

    /**
     * 获取前N月的最后一天
     */
    public static String getLastMaxMonthDate(int n) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -n);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime()) + " 23:59:59";
    }


    /**
     * 获取上个月的最后一天
     */
    public static String getLastMaxMonthDate() {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    /**
     * 获取上个月的第一天
     */
    public static String getStartMinMonthDate() {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.MONTH, -1);
        instance.set(Calendar.DAY_OF_MONTH, 1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(instance.getTime());
        return format1;
    }

    /**
     * 获取当前月开始和结束日期 00:00:00  、23:59:59
     */
    public static String[] getStartMonthEnd() {
        String[] sTime = new String[2];
        Calendar first = getFirstDayOfMonth(Calendar.getInstance());
        Calendar last = getlastDayOfMonth(Calendar.getInstance());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        sTime[0] = format.format(first.getTime()) + " 00:00:00";
        sTime[1] = format.format(last.getTime()) + " 23:59:59";
        return sTime;
    }

    /**
     * 返回指定年月的月的第一天
     */
    public static Date getFirstDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month - 1, 1);
        return calendar.getTime();
    }

    /**
     * 返回指定年月的月的最后一天
     */
    public static Date getLastDayOfMonth(Integer year, Integer month) {
        Calendar calendar = Calendar.getInstance();
        if (year == null) {
            year = calendar.get(Calendar.YEAR);
        }
        if (month == null) {
            month = calendar.get(Calendar.MONTH);
        }
        calendar.set(year, month - 1, 1);
        calendar.roll(Calendar.DATE, -1);
        return calendar.getTime();
    }

    /**
     * 取得某年某月第一天及最后天
     *
     * @return 结果不带00:00:00、23:59:59
     */
    public static String[] getStartAndEndDayByYearMonth(Integer year, Integer month) {
        String[] sTime = new String[2];
        sTime[0] = toYyyyMmDd(getFirstDayOfMonth(year, month));
        sTime[1] = toYyyyMmDd(getLastDayOfMonth(year, month));
        return sTime;
    }

    /**
     * 获取当前年份
     */
    public static Integer getCurrYear() {
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        return year;
    }

    /**
     * 获取对当前第属于第几周
     */
    public static Integer getCurrWeek() {
        Calendar cal = Calendar.getInstance();
        int week = cal.get(Calendar.WEEK_OF_YEAR);
        return week;
    }

    /**
     * 获取当前月份
     */
    public static Integer getCurrMonth() {
        Calendar cale = Calendar.getInstance();
        int month = cale.get(Calendar.MONTH) + 1;
        return month;
    }

    /**
     * 获取当年到目前为止月份
     */
    public static String[] getBeforeAllMonth(String year) {
        SimpleDateFormat dft2 = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        int currentMonth = Integer.valueOf(dft2.format(calendar.getTime()).substring(5, 7));
        String[] months = new String[currentMonth];
        String str = "";
        for (int i = 0; i < currentMonth; i++) {
            if (i + 1 < 10) {
                str = "0" + (i + 1);
            }
            months[i] = year + "-" + str;
        }
        return months;
    }

    /**
     * 获取当年到目前为止月份
     */
    public static String[] getYearAllMonth(String year) {
        String[] months = new String[12];
        String str = "";
        for (int i = 1; i <= 12; i++) {
            if (i < 10) {
                str = "0" + i;
            } else {
                str = i + "";
            }
            months[i - 1] = year + "-" + str;
        }
        return months;
    }

    /**
     * 根据指定日期计算所在周的周一和周日
     */
    public static Map<String, String> getDesignWeekDay(String date) throws ParseException {
        Map<String, String> map = new HashMap<String, String>();
        //设置时间格式
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        Date time = sdf.parse(date);
        cal.setTime(time);
        //判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        //获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        //设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        //获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        //根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);

        map.put("mon", DateUtil.getDateToStr(cal.getTime(), false));
        cal.add(Calendar.DATE, 6);
        map.put("sun", DateUtil.getDateToStr(cal.getTime(), true));
        return map;


    }

    /**
     * 判断是格式是否HH:mm:ss的时间
     */
    public static boolean isTime(String time) {
        if (time.matches("(0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})")) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 前后N天的时间0点
     *
     * @param days -n:向前n天 n:往后后n天
     */
    public static Date getDayAddOrReduceSeparatorTime(Calendar calendar, int days) {
        calendar.add(Calendar.DATE, days);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime();
    }

    /**
     * 判断纯日期类型，支持的格式为YYYY-MM-DD HH:mm:ss
     */
    public static boolean isDateTime(String datetime) {
        return pattern.matcher(datetime).matches();
    }

    public static Date dateToMongoDate(String dateStr) {
        Date parse = null;
        try {
            Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS'Z'");
            parse = format.parse(format.format(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }

    public static LocalDate parseLocalDate(Date date){
        Instant instant =  date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        return  instant.atZone(zoneId).toLocalDate();
    }

    public static LocalDate parseLocalDate(String date){
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern(parsePatterns[1]);
        return LocalDate.parse(date, fmt);
    }


    public static Date asDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date asDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static LocalDate asLocalDate(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static LocalDateTime asLocalDateTime(Date date) {
        return Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
    }

    public static String format(LocalDateTime time){
        if (time == null) {
            return "";
        }
        DateTimeFormatter df = DateTimeFormatter.ofPattern(parsePatterns[1]);
        return  df.format(time);
    }


    public static Date addDayOfDate(Date date,int i){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, i);
        Date newDate = c.getTime();
        return newDate;
    }

    public static Date addMinuteOfDate(Date date,int i){
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, i);
        Date newDate = c.getTime();
        return newDate;
    }

    public static long minutesBetween(Date startTime, Date endTime){
        return ChronoUnit.MINUTES.between(Instant.ofEpochMilli(startTime.getTime()), Instant.ofEpochMilli(endTime.getTime()));
    }

    /**
     * 获取一个月有多少天
     */
    public static int getDaysOfMonth(String date)  {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt;
        try {
            dt = sdf.parse(date);
        } catch (ParseException e) {
            e.getErrorOffset();
            return 0;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dt);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static int differentDaysByMillisecond(String date1, String date2)  {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt1;
        Date dt2;
        try {
            dt1 = sdf.parse(date1);
            dt2 = sdf.parse(date2);
        } catch (ParseException e) {
            e.printStackTrace();
            return 0;
        }
        int days = (int) ((dt2.getTime() - dt1.getTime()) / (1000*3600*24));
        return days;
    }

    /**
     * 判断某一时间是否在一个区间内
     *
     * @param sourceTime
     *            时间区间,半闭合,如[10:00-20:00]
     * @param curTime
     *            需要判断的时间 如10:00
     * @return
     * @throws IllegalArgumentException
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now > end && now < start) {
                    return false;
                } else {
                    return true;
                }
            }
            else {
                if (now >= start && now <= end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
    }

    public static boolean isInTime(String startTime, String endTime, String inTime, String pattern) {
        SimpleDateFormat simpleDate = new SimpleDateFormat(pattern);
        try {
            //  10:00  11:00 = > 10:15
            Date startDate = simpleDate.parse(startTime);
            Date endDate   = simpleDate.parse(endTime);
            Date inDate    = simpleDate.parse(inTime);
            return startDate.before(inDate) && endDate.after(inDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static boolean isInTime(String startTime, String endTime, Date inTime, String pattern) {
        SimpleDateFormat simpleDate = new SimpleDateFormat(pattern);
        try {
            //  10:00  11:00 = > 10:15
            Date startDate = simpleDate.parse(startTime);
            Date endDate   = simpleDate.parse(endTime);
            Date inDate    = simpleDate.parse(simpleDate.format(inTime));
            return startDate.before(inDate) && endDate.after(inDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

}
