package com.docus.server.collect.web.utils;

import lombok.extern.slf4j.Slf4j;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 日期工具类
 * @Author zengdl
 */
@Slf4j
public class DateUtils {

    /** 缺省日期格式 */
    public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

    /** 缺省时间格式 */
    public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

    /** 缺省月格式 */
    public static final String DEFAULT_MONTH = "MONTH";

    /** 缺省年格式 */
    public static final String DEFAULT_YEAR = "YEAR";

    /** 缺省日格式 */
    public static final String DEFAULT_DATE = "DAY";

    /** 缺省小时格式 */
    public static final String DEFAULT_HOUR = "HOUR";

    /** 缺省分钟格式 */
    public static final String DEFAULT_MINUTE = "MINUTE";

    /** 缺省秒格式 */
    public static final String DEFAULT_SECOND = "SECOND";

    /** 缺省长日期格式 */
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH-mm";

    /** 缺省长日期格式,精确到秒 */
    public static final String DEFAULT_DATETIME_FORMAT_SEC = "yyyy-MM-dd HH:mm:ss";

    /** 星期数组 */
    private static final String[] WEEKS = { "星期日", "星期一", "星期二", "星期三", "星期四",
            "星期五", "星期六" };

    /**
     * 取当前日期的字符串表示
     *
     * @return 当前日期的字符串 ,如2010-05-28
     **/
    public static String today() {
        return today(DEFAULT_DATE_FORMAT);
    }

    /**
     * 根据输入的格式得到当前日期的字符串
     *
     * @param strFormat
     *            日期格式
     * @return
     */
    public static String today(String strFormat) {
        return toString(new Date(), strFormat);
    }
    /**
     * 获取当前时间到指定时刻前的毫秒数
     * @param hour 指定时刻的小时
     * @param min 指定时刻的分钟
     * @param sec 指定时刻的秒
     * @param mill 指定时刻的毫秒
     * @return
     */
    public static long getMillsecBeforeMoment(int hour,int min,int sec,int mill){
        return getMillisecBetweenDate(new Date(),getMoment(hour,min,sec,mill));
    }

    /**
     * 获取当天的某一时刻Date
     * @param hour 24小时
     * @param min 分钟
     * @param sec 秒
     * @param mill 毫秒
     * @return
     */
    public static Date getMoment(int hour,int min,int sec,int mill){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY,hour);
        calendar.set(Calendar.MINUTE,min);
        calendar.set(Calendar.SECOND,sec);
        calendar.set(Calendar.MILLISECOND,mill);
        return calendar.getTime();
    }

    /**
     * 获取两个日期之间的毫秒数
     * @param before
     * @param after
     * @return
     */
    public static long getMillisecBetweenDate(Date before, Date after){
        long beforeTime = before.getTime();
        long afterTime = after.getTime();
        return afterTime - beforeTime;
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 取当前时间的字符串表示,
     *
     * @return 当前时间,如:21:10:12
     **/
    public static String currentTime() {
        return currentTime(DEFAULT_TIME_FORMAT);
    }

    /**
     * 根据输入的格式获取时间的字符串表示 'hh:mm:ss'
     *
     * @return 当前时间,如:21:10:12
     **/

    public static String currentTime(String strFormat) {
        return toString(new Date(), strFormat);
    }

    /**
     * 取得相对于当前时间增加天数/月数/年数后的日期 <br>
     * 欲取得当前日期5天前的日期,可做如下调用:<br>
     * getAddDay("DATE", -5).
     *
     * @param field
     *            ,段,如"year","month","date",对大小写不敏感
     * @param amount
     *            ,增加的数量(减少用负数表示),如5,-1
     * @return 格式化后的字符串 如"2010-05-28"
     * @throws ParseException
     **/

    public static String getAddDay(String field, int amount)
            throws ParseException {
        return getAddDay(field, amount, null);
    }

    /**
     * 取得相对于当前时间增加天数/月数/年数后的日期,按指定格式输出
     *
     * 欲取得当前日期5天前的日期,可做如下调用:<br>
     * getAddDay("DATE", -5,'yyyy-mm-dd hh:mm').
     *
     * @param field
     *            ,段,如"year","month","date",对大小写不敏感
     * @param amount
     *            ,增加的数量(减少用负数表示),如5,-1
     * @param strFormat
     *            ,输出格式,如"yyyy-mm-dd","yyyy-mm-dd hh:mm"
     * @return 格式化后的字符串 如"2010-05-28"
     * @throws ParseException
     **/
    public static String getAddDay(String field, int amount, String strFormat)
            throws ParseException {
        return getAddDay(null, field, amount, strFormat);
    }

    /**
     * 功能：对于给定的时间增加天数/月数/年数后的日期,按指定格式输出
     *
     * @param date
     *            String 要改变的日期
     * @param field
     *            int 日期改变的字段，YEAR,MONTH,DAY
     * @param amount
     *            int 改变量
     * @param strFormat
     *            日期返回格式
     * @return
     * @throws ParseException
     */
    public static String getAddDay(String date, String field, int amount,
                                   String strFormat) throws ParseException {
        if (strFormat == null) {
            strFormat = DEFAULT_DATETIME_FORMAT_SEC;
        }
        Calendar rightNow = Calendar.getInstance();
        if (date != null && !"".equals(date.trim())) {
            rightNow.setTime(parseDate(date, strFormat));
        }
        if (field == null) {
            return toString(rightNow.getTime(), strFormat);
        }
        rightNow.add(getInterval(field), amount);
        return toString(rightNow.getTime(), strFormat);
    }

    /**
     * 获取时间间隔类型
     *
     * @param field
     *            时间间隔类型
     * @return 日历的时间间隔
     */
    protected static int getInterval(String field) {
        String tmpField = field.toUpperCase();
        if (tmpField.equals(DEFAULT_YEAR)) {
            return Calendar.YEAR;
        } else if (tmpField.equals(DEFAULT_MONTH)) {
            return Calendar.MONTH;
        } else if (tmpField.equals(DEFAULT_DATE)) {
            return Calendar.DATE;
        } else if (DEFAULT_HOUR.equals(tmpField)) {
            return Calendar.HOUR;
        } else if (DEFAULT_MINUTE.equals(tmpField)) {
            return Calendar.MINUTE;
        } else {
            return Calendar.SECOND;
        }
    }

    /**
     * 获取格式化对象
     *
     * @param strFormat
     *            格式化的格式 如"yyyy-MM-dd"
     * @return 格式化对象
     */
    public static SimpleDateFormat getSimpleDateFormat(String strFormat) {
        if (strFormat != null && !"".equals(strFormat.trim())) {
            return new SimpleDateFormat(strFormat);
        } else {
            return new SimpleDateFormat();
        }
    }

    /**
     * 得到当前日期的星期数
     *
     * @return 当前日期的星期的字符串
     * @throws ParseException
     */
    public static String getWeekOfMonth() throws ParseException {
        return getWeekOfMonth(null, null);
    }

    /**
     * 根据日期的到给定日期的在当月中的星期数
     *
     * @param date
     *            给定日期
     * @return
     * @throws ParseException
     */
    public static String getWeekOfMonth(String date, String fromat)
            throws ParseException {
        Calendar rightNow = Calendar.getInstance();
        if (date != null && !"".equals(date.trim())) {
            rightNow.setTime(parseDate(date, fromat));
        }
        return WEEKS[rightNow.get(Calendar.WEEK_OF_MONTH)];
    }

    /**
     * 将java.util.date型按照指定格式转为字符串
     *
     * @param date
     *            源对象
     * @param format
     *            想得到的格式字符串
     * @return 如：2010-05-28
     */
    public static String toString(Date date, String format) {
        return getSimpleDateFormat(format).format(date);
    }

    /**
     * 将java.util.date型按照缺省格式转为字符串
     *
     * @param date
     *            源对象
     * @return 如：2010-05-28
     */
    public static String toString(Date date) {
        return toString(date, DEFAULT_DATE_FORMAT);
    }

    /**
     * 强制类型转换 从串到日期
     *
     * @param strDate
     *            源字符串，采用yyyy-MM-dd格式
     * @param format
     *            ps
     * @return 得到的日期对象
     * @throws ParseException
     */
    public static Date parseDate(String strDate, String format)
            throws ParseException {
        return getSimpleDateFormat(format).parse(strDate);
    }

    /***
     * 根据传入的毫秒数和格式，对日期进行格式化输出
     *
     * @version 2011-7-12
     * @param millisecond
     * @param format
     * @return
     */
    public static String millisecondFormat(Long millisecond, String format) {
        if (millisecond == null || millisecond <= 0) {
            throw new IllegalArgumentException(String.format("传入的时间毫秒数[%s]不合法",
                    "" + millisecond));
        }
        if (format == null || "".equals(format.trim())) {
            format = DEFAULT_DATE_FORMAT;
        }
        return toString(new Date(millisecond), format);
    }

    /**
     * 强制类型转换 从串到时间戳
     *
     * @param strDate
     *            源串
     * @param format
     *            遵循格式
     * @return 取得的时间戳对象
     * @throws ParseException
     */
    public static Timestamp parseTimestamp(String strDate, String format)
            throws ParseException {
        Date utildate = getSimpleDateFormat(format).parse(strDate);
        return new Timestamp(utildate.getTime());
    }

    /**
     * getCurDate 取当前日期
     *
     * @return java.util.Date型日期
     **/
    public static Date getCurDate() {
        return (new Date());
    }

    /**
     * getCurTimestamp 取当前时间戳
     *
     * @return java.sql.Timestamp
     **/
    public static Timestamp getCurTimestamp() {
        return new Timestamp(System.currentTimeMillis());
    }

    /**
     * getCurTimestamp 取遵循格式的当前时间
     *
     * @param format
     *            遵循格式
     * @return java.sql.Timestamp
     **/
    public static Date getCurDate(String format) throws Exception {
        return getSimpleDateFormat(format).parse(toString(new Date(), format));
    }

    /**
     * Timestamp按照指定格式转为字符串
     *
     * @param timestamp
     *            源对象
     * @param format
     *            ps（如yyyy.mm.dd）
     * @return 如：2010-05-28 或2010-05-281 13:21
     */
    public static String toString(Timestamp timestamp, String format) {
        if (timestamp == null) {
            return "";
        }
        return toString(new Date(timestamp.getTime()), format);
    }

    /**
     * Timestamp按照缺省格式转为字符串
     *
     * @param ts
     *            源对象
     * @return 如：2010-05-28
     */
    public static String toString(Timestamp ts) {
        return toString(ts, DEFAULT_DATE_FORMAT);
    }

    /**
     * Timestamp按照缺省格式转为字符串，可指定是否使用长格式
     *
     * @param timestamp
     *            欲转化之变量Timestamp
     * @param fullFormat
     *            是否使用长格式
     * @return 如：2010-05-28 或2010-05-28 21:21
     */
    public static String toString(Timestamp timestamp, boolean fullFormat) {
        if (fullFormat) {
            return toString(timestamp, DEFAULT_DATETIME_FORMAT_SEC);
        } else {
            return toString(timestamp, DEFAULT_DATE_FORMAT);
        }
    }

    /**
     * 将sqldate型按照指定格式转为字符串
     *
     * @param sqldate
     *            源对象
     * @param sFormat
     *            ps
     * @return 如：2010-05-28 或2010-05-28 00:00
     */
    public static String toString(java.sql.Date sqldate, String sFormat) {
        if (sqldate == null) {
            return "";
        }
        return toString(new Date(sqldate.getTime()), sFormat);
    }

    /**
     * 将sqldate型按照缺省格式转为字符串
     *
     * @param sqldate
     *            源对象
     * @return 如：2010-05-28
     */
    public static String toString(java.sql.Date sqldate) {
        return toString(sqldate, DEFAULT_DATE_FORMAT);
    }

    /**
     * 计算日期时间之间的差值， date1得时间必须大于date2的时间
     *
     * @version 2011-7-12
     * @param date1
     * @param date2
     * @return {@link Map} Map的键分别为, day(天),
     *         hour(小时),minute(分钟)和second(秒)。
     */
    public static Map<String, Long> timeDifference(final Date date1,
                                                   final Date date2) {
        if (date1 == null || date2 == null) {
            throw new NullPointerException("date1 and date2 can't null");
        }
        long mim1 = date1.getTime();
        long mim2 = date2.getTime();
        if (mim1 < mim2) {
            throw new IllegalArgumentException(String.format(
                    "date1[%s] not be less than date2[%s].", mim1 + "", mim2
                            + ""));
        }
        long m = (mim1 - mim2 + 1) / 1000L;
        long mday = 24L * 3600L;
        final Map<String, Long> map = new HashMap<String, Long>(16);
        map.put("day", m / mday);
        m = m % mday;
        map.put("hour", (m) / 3600);
        map.put("minute", (m % 3600) / 60);
        map.put("second", (m % 3600 % 60));
        return map;
    }

    public static Map<String, Integer> compareTo(final Date date1,
                                                 final Date date2) {
        if (date1 == null || date2 == null) {
            return null;
        }
        long time1 = date1.getTime();
        long time2 = date2.getTime();
        long time = Math.max(time1, time2) - Math.min(time1, time2);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time);
        Map<String, Integer> map = new HashMap<String, Integer>(16);
        map.put("year", (calendar.get(Calendar.YEAR) - 1970) > 0 ? (calendar
                .get(Calendar.YEAR) - 1970) : 0);
        map.put("month", (calendar.get(Calendar.MONTH) - 1) > 0 ? (calendar
                .get(Calendar.MONTH) - 1) : 0);
        map.put("day",
                (calendar.get(Calendar.DAY_OF_MONTH) - 1) > 0 ? (calendar
                        .get(Calendar.DAY_OF_MONTH) - 1) : 0);
        map.put("hour",
                (calendar.get(Calendar.HOUR_OF_DAY) - 8) > 0 ? (calendar
                        .get(Calendar.HOUR_OF_DAY) - 8) : 0);
        map.put("minute", calendar.get(Calendar.MINUTE) > 0 ? calendar
                .get(Calendar.MINUTE) : 0);
        map.put("second", calendar.get(Calendar.SECOND) > 0 ? calendar
                .get(Calendar.SECOND) : 0);
        return map;
    }

    public static Timestamp stringOrtimestamp(String reStr){
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        try {
            ts = Timestamp.valueOf(reStr+" 00:00:00");
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ts;
    }

    /**
     * 根据传入月份查询
     * @param length
     * @return
     */
    public static List<String> getAllYear(int length) {
        List<String> list = new ArrayList<>();

        String[] months = new String[length];
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));
        // 加一行代码,否则3月重复
        cal.set(Calendar.DATE,1);
        for (int i = 0; i < length; i++) {
            months[length - i - 1] = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1);
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        }
        for (int i = 0; i < months.length; i++) {
            list.add(months[i]);
        }

        return list;
    }

    /**
     * 根据传入月份查询
     * 从某月开始后退
     * @param length
     * @return
     */
    public static List<String> getAllYear(int length,String str,String split) {
        List<String> list = new ArrayList<>();
        String[] months = new String[length];

        Calendar cal = Calendar.getInstance();
        if(split.equals("-")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date date = null;
            try {
                date = sdf.parse(str);
            } catch (ParseException e) {
                log.error(e.getMessage());
            }
            cal.setTime(date);
        }else{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            Date date = null;
            try {
                date = sdf.parse(str);
            } catch (ParseException e) {
                log.error(e.getMessage());
            }
            cal.setTime(date);
        }
        // 加一行代码,否则3月重复
        cal.set(Calendar.DATE,1);
        for (int i = 0; i < length; i++) {
            months[length - i - 1] = cal.get(Calendar.YEAR) + "-" + (cal.get(Calendar.MONTH) + 1);

            if(split.equals("+")){
                months[length - i - 1] = cal.get(Calendar.YEAR) + "" + (cal.get(Calendar.MONTH) + 1);
            }
            if((cal.get(Calendar.MONTH) + 1 < 10)){
                months[length - i - 1] = cal.get(Calendar.YEAR) + "0" + (cal.get(Calendar.MONTH) + 1);

                if(split.equals("-")){
                    months[length - i - 1] = cal.get(Calendar.YEAR) + "-0" + (cal.get(Calendar.MONTH) + 1);
                }
            }

            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        }

        for (int i = 0; i < months.length; i++) {
            list.add(months[i]);
        }

        return list;
    }

    /**
     * 根据传入长度查询更精确月份时间
     * @param length 从str时间后退几个月
     * @param str 从哪个月份开始
     * @param splt 如果格式为 2019-01-01 则传入 - 否则传入 +
     * @return
     */
    public static List<String> getAllYearPrecision(int length,String str,String splt) {
        List<String> list = new ArrayList<>();
        //如果str不为空,就处理
        int year = 0;
        int month = 0;

        String[] months = new String[length];
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH));

        if(str!=null&&!"".equals(str)){
            if(splt.equals("-")){
                String split[] = str.split(splt);
                year = Integer.parseInt(split[1]);
                month = Integer.parseInt(split[0]);
            } else if(splt.equals("+")){
                year = Integer.parseInt(str.substring(0,4));
                month = Integer.parseInt(str.substring(4,6));
            }
            cal.set(Calendar.YEAR,year);
            cal.set(Calendar.MONTH,month);

        }
        // 加一行代码,否则3月重复
        cal.set(Calendar.DATE,1);
        for (int i = 0; i < length; i++) {
            months[length - i - 1] = cal.get(Calendar.YEAR) + "" + (cal.get(Calendar.MONTH) + 1);
            if((cal.get(Calendar.MONTH) + 1 < 10)){
                months[length - i - 1] = cal.get(Calendar.YEAR) + "0" + (cal.get(Calendar.MONTH) + 1);
            }
            cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - 1);
        }
        for (int i = 0; i < months.length; i++) {
            list.add(months[i]);
        }

        return list;
    }

    public static void main(String[] args) {
        List<String> list = getAllYear(3,"201901","+");

        for (String str : list) {
            String[] split = str.split("-");
            System.out.println(split[1]);
        }
        System.out.println(getAllYear(3,"201901","+"));
    }

    /**
     * 获取今年12个月数据
     * @param year
     * @return
     */
    public static List<String> getYearMonth(int year) {
        List<String> list = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            list.add(year+"-"+i);
        }

        return list;
    }

    /**
     * 传入年份月份,获取时间
     * @param year
     * @param month
     * @param value 需要减少的月份
     * @return
     */
    public static String getCal(int year,int month,int day,int value) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
        calendar.set(Calendar.MONTH, month-12);//当前时间前去一个月，即一个月前的时间    
        calendar.set(Calendar.DAY_OF_MONTH, day);//当前时间前去一个月，即一个月前的时间    
        return sdf.format(calendar.getTime());
    }

    /**
     * 传入时间得到12个月前数据
     * @return
     */
    public static String getCal(Date date,int value) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -value);//当前时间前去一个月，即一个月前的时间    
        return sdf.format(calendar.getTime());
    }

    public static Date stringToDate(String dstr,String format){
        SimpleDateFormat sdf=new SimpleDateFormat(format);//小写的mm表示的是分钟
        //String转date
        Date date = null;
        try {
            date = sdf.parse(dstr);
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        //Date转String
//        String date = formatter.format(new Date());
        return date;
    }

    /**
     * 字符数据格式化(暂只支持到年月日)
     * 且格式必须 如: 2019-09-09
     * @param dstr
     * @return
     */
    public static String stringFormat(String dstr){
        return dstr.replaceAll("-","");
    }

    /**
     * 传入开始结束时间得到所有时间参数
     * @param date1
     * @param date2
     * @return
     */
    public static List<String> getYear(Date date1,Date date2){
        List<String> list = new ArrayList<>();

        Calendar calendar=new GregorianCalendar();
        calendar.setTime(date1);

        Calendar scalendar=new GregorianCalendar();
        scalendar.setTime(date2);
        int year1=calendar.get(Calendar.YEAR);
        int year2=scalendar.get(Calendar.YEAR);

        for(int i= year2;i<=year1;i++){
            list.add(i+"");
        }

        return list;
    }
    /**
     * 月份计算 date1必须大于date2
     * @param date1
     * @param date2
     * @return
     */
    public static int getmonth(Date date1,Date date2){
        Calendar calendar=new GregorianCalendar();
        calendar.setTime(date1);

        Calendar scalendar=new GregorianCalendar();
        scalendar.setTime(date2);
        int year1=calendar.get(Calendar.YEAR);
        int year2=scalendar.get(Calendar.YEAR);
        int month1=calendar.get(Calendar.MONTH);
        int month2=scalendar.get(Calendar.MONTH);

        return coundM(year1,year2,month1,month2);
    }

    /**
     * 月份计算 date1必须大于date2
     * @param date1
     * @param date2
     * @splt 如果是201901格式的数据传入 +号 如果是2019-02格式数据传入 - 号
     * @return
     */
    public static int getmonth(String date1,String date2,String splt){
        int year1= 0;
        int year2= 0;
        int month1= 0;
        int month2= 0;
        if(splt.equals("-")){
            String dateStr1[] = date1.split("-");
            String dateStr2[] = date2.split("-");
            year1= Integer.parseInt(dateStr1[0]);
            year2= Integer.parseInt(dateStr2[0]);
            month1= Integer.parseInt(dateStr1[1]);
            month2= Integer.parseInt(dateStr2[1]);
        } else if(splt.equals("+")){
            year1 = Integer.parseInt(date1.substring(0,4));
            year2 = Integer.parseInt(date2.substring(0,4));
            month1 = Integer.parseInt(date1.substring(4,6));
            month2 = Integer.parseInt(date2.substring(4,6));
        }
        return coundM(year1,year2,month1,month2);
    }

    /**
     * 该方法经过特殊处理 1 - 12 月 = 12 月 算头算尾
     * @param year1
     * @param year2
     * @param month1
     * @param month2
     * @return
     */
    public static int coundM(int year1, int year2, int month1, int month2){
        int count = 0;

        if(year1==year2&&month1>month2){
            count= month1-(month2-1);
        }
        else if(year1==year2&&month1<month2){
            count= month2-month1;
        }
        else if(year1 >year2&&month1>month2){

            count= (year1-year2)*12+(month1-month2)+1;
        }
        else if(year1>year2&&month1<month2){
            //应业务需求改为 -1
            //count = (year1-year2) * 12 - (month2-month1) -1;
            //  count= year1-(year2+1)+(13-month2)+month1;
            count= (year1-year2)*12+month1-month2+1;
        }
        else if(year1<year2&&month1 >month2){
            count=(year2-year1 )*12+month1-month2;
        }
        else if(year1<year2&&month1<month2){
            count= (year2-year1)*12+month2-month1;
        }
        else if(year1>year2&&month1==month2){
            count= (year1-year2)*12 +1;
        }
        else if(year1==year2&&month1==month2){
            count= 1;
        }
        return count;
    }

    /**
     * 传入开始时间和月份计算结束时间
     * @param beginTime
     * @param month
     * @return
     */
    public static String dataStriing(String beginTime,Integer month){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//格式化输出日期
        Date dt = null;
        try {
            dt = sdf.parse(beginTime);
        } catch (ParseException e) {
            log.error(e.getMessage());
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, month);//日期加月份
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }
    private static final DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static boolean verifyDateTime(String dateTime) {
        try {
            dtf.parse(dateTime);
            return true;
        }catch (Exception ex){
            log.error(ex.getMessage(),ex);
            return false;
        }
    }
}
