package com.mosty.base.utils;

import com.mosty.common.base.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.logging.Logger;


/**
 * 日期转换处理类
 *
 * @author martin_wp
 */
@Slf4j
public class DateUtils implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    private static SimpleDateFormat defaultYearFormatter = new SimpleDateFormat(
            "yyyy");

    private static SimpleDateFormat defaultDateFormatter = new SimpleDateFormat(
            "yyyy-MM-dd");

    private static SimpleDateFormat defaultDateTimeFormatter = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    private static SimpleDateFormat defaultDateTimeCompactFormatter = new SimpleDateFormat(
            "yyyyMMddHHmmss");

    private static SimpleDateFormat defaultDateTimeMillionCompactFormatter = new SimpleDateFormat(
            "yyyyMMddHHmmssSSS");

    public static final SimpleDateFormat defaultTimeFormatter = new SimpleDateFormat(
            "HH:mm");

    public static final SimpleDateFormat defaultTimeFormatter1 = new SimpleDateFormat(
            "HH:mm:ss");

    private static SimpleDateFormat defaultTimeFormatter2 = new SimpleDateFormat(
            "yyyy-MM-dd HH");
    /**
     * 日期转换格式
     */
    private static SimpleDateFormat dateFormatter01 = new SimpleDateFormat(
            "yyyy-MM-dd");
    private static SimpleDateFormat dateFormatter02 = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat dateFormatter03 = new SimpleDateFormat(
            "yyyyMMdd");
    private static SimpleDateFormat dateFormatter04 = new SimpleDateFormat(
            "yyyyMMddHHmmss");
    private static SimpleDateFormat dateFormatter05 = new SimpleDateFormat(
            "yyyy-MM-dd HH");
    private static SimpleDateFormat dateFormatter06 = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm");
    private static SimpleDateFormat dateFormatter07 = new SimpleDateFormat(
            "yyyyMMddHH");
    private static SimpleDateFormat dateFormatter08 = new SimpleDateFormat(
            "yyyyMMddHHmm");
    private static SimpleDateFormat dateFormatter09 = new SimpleDateFormat(
            "HH:mm");
    private static SimpleDateFormat dateFormatter10 = new SimpleDateFormat(
            "HH:mm:ss");
    private static SimpleDateFormat dateFormatter11 = new SimpleDateFormat(
            "yyyy-MM");
    private static SimpleDateFormat dateFormatter12 = new SimpleDateFormat(
            "dd");
    private static SimpleDateFormat dateFormatter13 = new SimpleDateFormat(
            "yyyy");
    private static SimpleDateFormat dateFormatter14 = new SimpleDateFormat(
            "MM");
    private static SimpleDateFormat dateFormatter15 = new SimpleDateFormat(
            "yyyyMM");
    private static SimpleDateFormat dateFormatter16 = new SimpleDateFormat(
            "HH");
    private static SimpleDateFormat dateFormatter17 = new SimpleDateFormat(
            "MMdd");
    private static SimpleDateFormat dateFormatter18 = new SimpleDateFormat(
            "MM-dd");

    /**
     * 取得系统当前的年份,格式如: (2008-05-12 14:00:00)<br>
     *
     * @return String
     */
    public static String getHDateString() {
        return defaultTimeFormatter2.format(new Date())+":00:00";
    }

    /**
     * 取得系统当前的日期字符表达式,格式如: (2008-05-12)<br>
     *
     * @return String
     */
    public static String getSystemDateString() {
        return defaultDateFormatter.format(new Date());
    }


    public static String getSystemYearString() {
        return defaultYearFormatter.format(new Date());
    }



    /**
     * 取得系统当前的日期时间字符表达式,格式如: (2008-05-12 14:28:36)<br>
     *
     * @return String
     */
    public static String getSystemDateTimeString() {
        return defaultDateTimeFormatter.format(new Date());
    }

    /**
     * 取得系统当前的日期时间字符表达式,格式如: (20080512142836)<br>
     *
     * @return String
     */
    public static String getSystemDateTimeCompactString() {
        return defaultDateTimeCompactFormatter.format(new Date());
    }

    /**
     * 取得系统当前的日期时间字符表达式(带毫秒数),格式如: (20080512142836326)<br>
     *
     * @return String
     */
    public static String getSystemDateTimeMillionCompactString() {
        return defaultDateTimeMillionCompactFormatter
                .format(new Date());
    }



    /**
     * 取得一个日期的字符表达式,格式如: (2008-05-12)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getShortString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            returnValue = formatter.format(convertDate);
        }
        return returnValue;
    }

    /**
     * 取得一个日期时间的字符表达式,格式如: (2008-05-12 14:28:36)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getFullString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat(
                    "yyyy-MM-dd HH:mm:ss");
            returnValue = formatter.format(convertDate);
        }
        return returnValue;
    }

    /**
     * 计算两个日期之间相差的天数
     *
     * @param smdate 较小的时间
     * @param bdate  较大的时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date smdate, Date bdate) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        smdate = sdf.parse(sdf.format(smdate));
        bdate = sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days = (time2 - time1) / (1000 * 3600 * 24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    /**
     * 取得一个日期的紧凑字符表达式,格式如: (20080512)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getShortCompactString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            returnValue = formatter.format(convertDate);
        }
        return returnValue;
    }

    /**
     * 取得一个日期时间的紧凑字符表达式,格式如: (20080512142836)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getFullCompactString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            returnValue = formatter.format(convertDate);
        }
        return returnValue;
    }

    /**
     * 取得一个日期的中文字符表达式,格式如: (2008年5月12日)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getChineseShortString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.LONG);
            returnValue = dateFormat.format(convertDate);
        }
        return returnValue;
    }

    /**
     * 获取当前日期
     *
     * @return
     */
    public static String getNowDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String time = sdf.format(date);
        return time;
    }

    /**
     * 取得一个日期时间的中文字符表达式,格式如: (2008年05月12日14时)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getChineseMidString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String temp = formatter.format(convertDate);
            returnValue = temp.substring(0, 4) + "年" + temp.substring(4, 6)
                    + "月" + temp.substring(6, 8) + "日" + temp.substring(8, 10)
                    + "时";
        }
        return returnValue;
    }

    /**
     * 取得一个日期时间的中文字符表达式,格式如: (2008年05月12日14时28分)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getChineseMiddleString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String temp = formatter.format(convertDate);
            returnValue = temp.substring(0, 4) + "年" + temp.substring(4, 6)
                    + "月" + temp.substring(6, 8) + "日" + temp.substring(8, 10)
                    + "时" + temp.substring(10, 12) + "分";
        }
        return returnValue;
    }

    /**
     * 取得一个日期的中文字符表达式,包括星期,格式如: (2008年5月12日 星期一)<br>
     *
     * @param convertDate 需要转换的日期
     * @return String
     */
    public static String getChineseFullString(Date convertDate) {
        String returnValue = "";
        if (convertDate != null) {
            DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
            returnValue = dateFormat.format(convertDate);
        }
        return returnValue;
    }

    /**
     * 取得一个日期的纯中文字符表达式,格式如: (2008-05-12 转换后得到 二00八年五月十二日)<br>
     *
     * @param dateString 需要转换的日期字符串
     * @return String
     */
    public static String getPureChineseString(String dateString)
            throws Exception {
        String returnValue = "";
        if (dateString != null && !"".equals(dateString)) {
            String[] chinaNumber = new String[10];
            chinaNumber[0] = "0";
            chinaNumber[1] = "一";
            chinaNumber[2] = "二";
            chinaNumber[3] = "三";
            chinaNumber[4] = "四";
            chinaNumber[5] = "五";
            chinaNumber[6] = "六";
            chinaNumber[7] = "七";
            chinaNumber[8] = "八";
            chinaNumber[9] = "九";
            String tempString = "";
            try {
                tempString = dateString.substring(0, 4);
                for (int i = 0; i < tempString.length(); i++) {
                    int cn = Integer.parseInt(tempString.substring(i, i + 1));
                    returnValue += chinaNumber[cn];
                }
                returnValue += "年";
                tempString = dateString.substring(5, 7);
                for (int i = 0; i < tempString.length(); i++) {
                    int cn = Integer.parseInt(tempString.substring(i, i + 1));
                    if (i == 0) {
                        if (cn == 1) {
                            returnValue += "十";
                        }
                    } else {
                        if (cn > 0)
                            returnValue += chinaNumber[cn];
                    }
                }
                returnValue += "月";
                tempString = dateString.substring(8);
                for (int i = 0; i < tempString.length(); i++) {
                    int cn = Integer.parseInt(tempString.substring(i, i + 1));
                    if (i == 0) {
                        switch (cn) {
                            case 0:
                                break;
                            case 1:
                                returnValue += "十";
                                break;
                            default:
                                returnValue += chinaNumber[cn] + "十";
                                break;
                        }
                    } else {
                        if (cn > 0)
                            returnValue += chinaNumber[cn];
                    }

                }
                returnValue += "日";
            } catch (Exception ex) {
                returnValue = "";
                log.error("日期转换错误: 转换字符串: " + dateString + "，"
                        + ex.getMessage(), ex);
                throw new Exception("日期转换错误: 转换字符串: " + dateString + "，"
                        + ex.getMessage());
            }
        }
        return returnValue;
    }

    /**
     * 取得一个日期的纯中文字符表达式,格式如: (2008-05-12 转换后得到 二○○八年五月十二日)<br>
     *
     * @param dateString 需要转换的日期字符串
     * @return String
     */
    public static String getPureChineseString2(String dateString)
            throws Exception {
        String returnValue = "";
        if (dateString != null && !"".equals(dateString)) {
            String[] chinaNumber = new String[10];
            chinaNumber[0] = "○";
            chinaNumber[1] = "一";
            chinaNumber[2] = "二";
            chinaNumber[3] = "三";
            chinaNumber[4] = "四";
            chinaNumber[5] = "五";
            chinaNumber[6] = "六";
            chinaNumber[7] = "七";
            chinaNumber[8] = "八";
            chinaNumber[9] = "九";
            String tempString = "";
            try {
                tempString = dateString.substring(0, 4);
                for (int i = 0; i < tempString.length(); i++) {
                    int cn = Integer.parseInt(tempString.substring(i, i + 1));
                    returnValue += chinaNumber[cn];
                }
                returnValue += "年";
                tempString = dateString.substring(5, 7);
                for (int i = 0; i < tempString.length(); i++) {
                    int cn = Integer.parseInt(tempString.substring(i, i + 1));
                    if (i == 0) {
                        if (cn == 1) {
                            returnValue += "十";
                        }
                    } else {
                        if (cn > 0)
                            returnValue += chinaNumber[cn];
                    }
                }
                returnValue += "月";
                tempString = dateString.substring(8);
                for (int i = 0; i < tempString.length(); i++) {
                    int cn = Integer.parseInt(tempString.substring(i, i + 1));
                    if (i == 0) {
                        switch (cn) {
                            case 0:
                                break;
                            case 1:
                                returnValue += "十";
                                break;
                            default:
                                returnValue += chinaNumber[cn] + "十";
                                break;
                        }
                    } else {
                        if (cn > 0)
                            returnValue += chinaNumber[cn];
                    }

                }
                returnValue += "日";
            } catch (Exception ex) {
                returnValue = "";
                log.error("日期转换错误: 转换字符串: " + dateString + ","
                        + ex.getMessage(), ex);
                throw new Exception("日期转换错误: 转换字符串: " + dateString + ","
                        + ex.getMessage());
            }
        }
        return returnValue;
    }

    /**
     * 字符串转换成日期 (注意: dateString 与 formatString 要匹配)<br>
     *
     * @param dateString   需要转换的日期字符串
     * @param formatString 转换格式 yyyy-MM-dd HH:mm:ss 或 yyyy-MM-dd
     * @return java.util.Date
     */
    public static Date stringToDate(String dateString,
                                    String formatString) throws Exception {
        Date returnValue = null;
        try {
            if (dateString != null && !"".equals(dateString)) {
                SimpleDateFormat formatter = new SimpleDateFormat(formatString);
                formatter.setLenient(false); // 设置时间分析为严格的
                returnValue = formatter.parse(dateString);
            }
        } catch (Exception ex) {
            log.error("日期转换错误: " + dateString + " 指定格式 [" + formatString
                    + "]，" + ex.getMessage(), ex);
            throw new Exception("日期转换错误: " + dateString + " 指定格式 ["
                    + formatString + "]，" + ex.getMessage());
        }
        return returnValue;
    }

    /**
     * 取得指定的下一个日期<br>
     * <p>
     * 注意：按月运算时，如果运算后当月没有该日，则为当月的最后一天
     * </p>
     *
     * @param startDate 开始日期
     * @param nextType  运算类型（Y=年数；M=月数；D=天数；H=小时数；m=分钟数；S=秒数）
     * @param nextStep  运算长度（为负数指取以前的日期）
     * @return java.util.Date
     */
    public static Date getNextDate(Date startDate, String nextType,
                                   int nextStep) {
        Date endDate = startDate;
        if (startDate != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startDate);
            if ("Y".equals(nextType)) {
                calendar.add(Calendar.YEAR, nextStep);
            } else if ("M".equals(nextType)) {
                calendar.add(Calendar.MONTH, nextStep);
            } else if ("D".equals(nextType)) {
                calendar.add(Calendar.DATE, nextStep);
            } else if ("H".equals(nextType)) {
                calendar.add(Calendar.HOUR, nextStep);
            } else if ("m".equals(nextType)) {
                calendar.add(Calendar.MINUTE, nextStep);
            } else if ("S".equals(nextType)) {
                calendar.add(Calendar.SECOND, nextStep);
            }
            endDate = calendar.getTime();
        }
        return endDate;
    }

    /**
     * 字符串转时间
     *
     * @param dateType 时间格式
     * @param date     字符串时间
     * @return Date
     */
    public static Date strToDate(String date, String dateType) {
        Date returnValue = null;
        try {
            if ("01".equals(dateType)) {
                returnValue = dateFormatter01.parse(date);
            } else if ("02".equals(dateType)) {
                returnValue = dateFormatter02.parse(date);
            } else if ("03".equals(dateType)) {
                returnValue = dateFormatter03.parse(date);
            } else if ("04".equals(dateType)) {
                returnValue = dateFormatter04.parse(date);
            } else if ("05".equals(dateType)) {
                returnValue = dateFormatter05.parse(date);
            } else if ("06".equals(dateType)) {
                returnValue = dateFormatter06.parse(date);
            } else if ("07".equals(dateType)) {
                returnValue = dateFormatter07.parse(date);
            } else if ("08".equals(dateType)) {
                returnValue = dateFormatter08.parse(date);
            } else if ("09".equals(dateType)) {
                returnValue = dateFormatter09.parse(date);
            } else if ("10".equals(dateType)) {
                returnValue = dateFormatter10.parse(date);
            } else if ("11".equals(dateType)) {
                returnValue = dateFormatter11.parse(date);
            } else if ("12".equals(dateType)) {
                returnValue = dateFormatter12.parse(date);
            } else if ("13".equals(dateType)) {
                returnValue = dateFormatter13.parse(date);
            } else if ("14".equals(dateType)) {
                returnValue = dateFormatter14.parse(date);
            } else if ("15".equals(dateType)) {
                returnValue = dateFormatter15.parse(date);
            } else if ("16".equals(dateType)) {
                returnValue = dateFormatter16.parse(date);
            } else if ("17".equals(dateType)) {
                returnValue = dateFormatter17.parse(date);
            } else if ("18".equals(dateType)) {
                returnValue = dateFormatter18.parse(date);
            } else {
                returnValue = dateFormatter02.parse(date);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new BusinessException("日期转换异常");
        }
        return returnValue;
    }

    /**
     * 组合查询列表日期字段做格式转换（已在数据库中作转换，暂时不用）<br>
     *
     * @param date
     * @param dateType
     * @return String
     */
    public static String getQueryDateString(Date date, String dateType) {
        String returnValue = "";
        if (date != null) {
            if ("01".equals(dateType)) {
                returnValue = dateFormatter01.format(date);
            } else if ("02".equals(dateType)) {
                returnValue = dateFormatter02.format(date);
            } else if ("03".equals(dateType)) {
                returnValue = dateFormatter03.format(date);
            } else if ("04".equals(dateType)) {
                returnValue = dateFormatter04.format(date);
            } else if ("05".equals(dateType)) {
                returnValue = dateFormatter05.format(date);
            } else if ("06".equals(dateType)) {
                returnValue = dateFormatter06.format(date);
            } else if ("07".equals(dateType)) {
                returnValue = dateFormatter07.format(date);
            } else if ("08".equals(dateType)) {
                returnValue = dateFormatter08.format(date);
            } else if ("09".equals(dateType)) {
                returnValue = dateFormatter09.format(date);
            } else if ("10".equals(dateType)) {
                returnValue = dateFormatter10.format(date);
            } else if ("11".equals(dateType)) {
                returnValue = dateFormatter11.format(date);
            } else if ("12".equals(dateType)) {
                returnValue = dateFormatter12.format(date);
            } else if ("13".equals(dateType)) {
                returnValue = dateFormatter13.format(date);
            } else if ("14".equals(dateType)) {
                returnValue = dateFormatter14.format(date);
            } else if ("15".equals(dateType)) {
                returnValue = dateFormatter15.format(date);
            } else if ("16".equals(dateType)) {
                returnValue = dateFormatter16.format(date);
            } else if ("17".equals(dateType)) {
                returnValue = dateFormatter17.format(date);
            } else if ("18".equals(dateType)) {
                returnValue = dateFormatter18.format(date);
            } else {
                returnValue = dateFormatter02.format(date);
            }
        }
        return returnValue;
    }

    public static String getDate(int age) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date dt = null;
        try {
            dt = sdf.parse(sdf.format(new Date()));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.YEAR, age);
        Date dt1 = rightNow.getTime();
        return sdf.format(dt1);
    }

    /**
     * 比较2个日期间大小
     *
     * @param DATE1
     * @param DATE2
     * @return
     */
    public static boolean compareDate(String DATE1, String DATE2) {
        try {
            Date dt1 = dateFormatter02.parse(DATE1);
            Date dt2 = dateFormatter02.parse(DATE2);
            if (dt1.getTime() >= dt2.getTime()) {
                return false;
            } else if (dt1.getTime() <= dt2.getTime()) {
                return true;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return false;
    }

    //当前时间加定义天数
    public static String dateAdd(String dateTime, Integer sum) {
        String format = "";
        Date parse = null;
        try {
            parse = defaultDateTimeFormatter.parse(dateTime);
            Calendar cal = Calendar.getInstance();
            cal.setTime(parse);//设置起时间
            //cal.add(Calendar.YEAR, 1);//增加一年
            cal.add(Calendar.DATE, sum);//增加一天
            //cd.add(Calendar.DATE, -10);//减10天
            //cd.add(Calendar.MONTH, n);//增加一个月
            format = defaultDateTimeFormatter.format(cal.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return format;
    }


    //计算两个时间之间的差
    public static long  dateDay(String date1, String date2) {
        long days = 0;
        try {
            Date parse1 = defaultDateFormatter.parse(date1);
            Date parse2 = defaultDateFormatter.parse(date2);
            Long time = null;
            if (parse1.before(parse2)) {
                time = parse2.getTime() - parse1.getTime();
            } else {
                time = parse1.getTime() - parse2.getTime();
            }
            days = time / (1000 * 60 * 60 * 24);//天
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return days;
    }


    public static String dateGap(String date1, String date2) {
        String zqsc = "";
        try {
            Date parse1 = defaultDateTimeFormatter.parse(date1);
            Date parse2 = defaultDateTimeFormatter.parse(date2);
            Long time = null;
            if (parse1.before(parse2)) {
                time = parse2.getTime() - parse1.getTime();
            } else {
                time = parse1.getTime() - parse2.getTime();
            }
            long days = time / (1000 * 60 * 60 * 24);//天
            long hours = (time - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);//小时
            long minutes = (time - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);//分钟
            zqsc = days + "天" + hours + "小时" + minutes + "分钟";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return zqsc;
    }


    /**
     * 日期转星期
     *
     * @param datetime
     * @return
     */
    public static String dateToWeek(String datetime) {
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        // 获得一个日历
        Calendar cal = Calendar.getInstance();
        Date datet = null;
        try {
            datet = f.parse(datetime);
            cal.setTime(datet);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 指示一个星期中的某天。
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) {
            w = 0;
        }
        return weekDays[w];
    }

    /**
     * 添加月份
     *
     * @param sourceDate
     * @param month
     * @return
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);
        return c.getTime();
    }

    // 获取本月的最后一天
    public static Date getThisMonthLastDay() {
        Date date = new Date();
        // 获取下个月的第一天
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, +1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    // 获取指定月的最后一天
    public static Date getThisMonthLastDay(String time) {
        time = time + "-01";
        Date date = strToDate(time, "01");
        // 获取下个月的第一天
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_MONTH, +1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getTime();
    }

    /**
     * 日期转星期
     *
     * @param date
     * @return
     */
    public static int dateToWeek(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0) w = 0;
        return w;
    }

    public static Long dayTimeInMillis() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTimeInMillis();

    }


    /**
     * 当前时间加定义天数
     * @param type 01 年 02 月 03 天
     * @param sum
     * @return
     */
    public static String dateTypeAdd(String type, Integer sum) {
        String format = "";
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(new Date());//设置起时间
            if("01".equals(type)) {
                cal.add(Calendar.YEAR, sum);//增加一年
            } else if("02".equals(type)) {
                cal.add(Calendar.MONTH, sum);//增加一月
            } else if("03".equals(type)) {
                cal.add(Calendar.DATE, sum);//增加一天
            } else if("04".equals(type)) {
                cal.add(Calendar.YEAR, -sum);//减一年
            } else if("05".equals(type)) {
                cal.add(Calendar.MONTH, -sum);//减一月
            } else if("06".equals(type)) {
                cal.add(Calendar.DATE, -sum);//增减一天
            }
            //cd.add(Calendar.DATE, -10);//减10天
            format = defaultDateFormatter.format(cal.getTime());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return format;
    }

    //日期转化方法
    /**
     * mongo 日期查询isodate
     * @param dateStr
     * @return
     */
    public static Date dateToISODate(String dateStr){
        //T代表后面跟着时间，Z代表UTC统一时间
        Date date = formatD(dateStr);
        SimpleDateFormat format =
                new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        format.setCalendar(new GregorianCalendar(new SimpleTimeZone(0, "GMT")));
        String isoDate = format.format(date);
        try {
            return format.parse(isoDate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    /** 时间格式(yyyy-MM-dd HH:mm:ss) */
    public final static String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static Date formatD(String dateStr){
        return formatD(dateStr,DATE_TIME_PATTERN);
    }

    public static Date formatD(String dateStr ,String format)  {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date ret = null ;
        try {
            ret = simpleDateFormat.parse(dateStr) ;
        } catch (ParseException e) {
            //
        }
        return ret;
    }



    public static void main(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date(1693584000000L));


        System.out.println();
    }
}
