时间处理工具类

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @desc:时间处理工具类
 */
public class SSHDateUtils {
    private static final String[] weeks = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};

    /**获取当前月的时间
     * @return
     */
    public static Date getMonth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        String first = format.format(new Date());
        try {
            return format.parse(first);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**获取当前月的第一天
     * @return
     */
    public static Date getFirstDayOfMonth(){
        SimpleDateFormat format = new SimpleDateFormat(SSHDateFormatUtils.DATE_FORMAT1);
        //获取当前月第一天:
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH,1);
        String first = format.format(c.getTime());
        try {
            return format.parse(first);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**获取当前月的最后一天
     * @return
     */
    public static Date getFinalDayOfMonth(){
        SimpleDateFormat format = new SimpleDateFormat(SSHDateFormatUtils.DATE_FORMAT1);
        //获取当前月第一天:
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, 0);
        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH,daysOfMonth(new Date()));
        String first = format.format(c.getTime());
        try {
            return format.parse(first);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**比较两个日期是否相等  忽略时分秒
     * @param date
     * @param date2
     * @return
     */
    public static boolean sameDate(Date date, Date date2) {
        LocalDate localDate1 = ZonedDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault()).toLocalDate();
        LocalDate localDate2 = ZonedDateTime.ofInstant(date2.toInstant(), ZoneId.systemDefault()).toLocalDate();
        return localDate1.isEqual(localDate2);
    }

    /** 获取日期格式为yyyy-MM-dd HH:mm:ss 的下一天
     * @param date
     * @return
     */
    public static Date getBeforeDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date nextDate = new Date(calendar.getTimeInMillis());
        return dateSimpleDateFormat(nextDate);
    }

    /** 获取日期格式为yyyy-MM-dd HH:mm:ss 的下一天
     * @param date
     * @return
     */
    public static Date getNextDate(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        Date nextDate = new Date(calendar.getTimeInMillis());
        return dateSimpleDateFormat(nextDate);
    }

    /** 转化任意日期为yyyy-MM-dd
     * @param date 需要转化的日期
     * @return
     */
    public static Date dateSimpleDateFormatOfDay(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SSHDateFormatUtils.DATE_FORMAT1);
        String dateString = simpleDateFormat.format(date);
        Date date1 = null;
        try {
            date1 = simpleDateFormat.parse(dateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }

    /** 转化任意日期为yyyy-MM-dd HH:mm:ss
     * @param date 需要转化的日期
     * @return
     */
    public static Date dateSimpleDateFormat(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(SSHDateFormatUtils.DATE_FORMAT2);
        String dateString = simpleDateFormat.format(date);
        return string2Date(dateString);
    }

    /**
     * 判断该日期是否是该月的第一天
     *
     * @param date
     *            需要判断的日期
     * @return
     */
    public static boolean isFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH) == 1;
    }

    /**
     * 判断该月有多少天
     *
     * @param date
     *            需要判断的日期
     * @return
     */
    public static int daysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 根据指定格式获取当前时间
     *
     * @param format
     * @return String
     */
    public static String getCurrentTime(String format) {
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);
        Date date = new Date();
        return sdf.format(date);
    }

    public static  String LongToDateString(Long value){
        Date date = new Date(value);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return  format.format(date);
    }

    public static  String stringToDate(Date value){

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return  format.format(value);
    }

    public static  String dateTostring(Date value){

        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
        return  format.format(value);
    }

    /**
     * @Author:zuoyf
     * @Description:根据一个字符串返回毫秒
     * @CreateDate:2018/5/29_15:37
     */
    public static  Long date2Long(String date){
        try{
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return  format.parse(date).getTime();
        }catch(Exception e){e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
            return null;
        }
    }

    /**
     * 获取当前时间,格式为:yyyy-MM-dd HH:mm:ss
     *
     * @return String
     */
    public static String getCurrentTime() {
        return getCurrentTime(SSHDateFormatUtils.DATE_FORMAT2);
    }

    /**
     * 获取指定格式的当前时间:为空时格式为yyyy-mm-dd HH:mm:ss
     *
     * @param format
     * @return Date
     */
    public static Date getCurrentDate(String format) {
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);
        String dateS = getCurrentTime(format);
        Date date = null;
        try {
            date = sdf.parse(dateS);
        } catch (ParseException e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
        }
        return date;
    }

    /**
     * 获取当前时间,格式为yyyy-MM-dd HH:mm:ss
     *
     * @return Date
     */
    public static Date getCurrentDate() {
        return getCurrentDate(SSHDateFormatUtils.DATE_FORMAT2);
    }

    /**
     * 给指定日期加入年份,为空时默认当前时间
     *
     * @param year   年份  正数相加、负数相减
     * @param date   为空时,默认为当前时间
     * @param format 默认格式为:yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String addYearToDate(int year, Date date, String format) {
        Calendar calender = getCalendar(date, format);
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);

        calender.add(Calendar.YEAR, year);

        return sdf.format(calender.getTime());
    }

    /**
     * 给指定日期加入年份,为空时默认当前时间
     *
     * @param year   年份  正数相加、负数相减
     * @param date   为空时,默认为当前时间
     * @param format 默认格式为:yyyy-MM-dd HH:mm:ss
     * @return String
     */
    public static String addYearToDate(int year, String date, String format) {
        Date newDate = new Date();
        if (null != date && !"".equals(date)) {
            newDate = string2Date(date, format);
        }

        return addYearToDate(year, newDate, format);
    }

    /**
     * 给指定日期增加月份 为空时默认当前时间
     *
     * @param month  增加月份  正数相加、负数相减
     * @param date   指定时间
     * @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addMothToDate(int month, Date date, String format) {
        Calendar calender = getCalendar(date, format);
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);

        calender.add(Calendar.MONTH, month);

        return sdf.format(calender.getTime());
    }

    /**
     * 给指定日期增加月份 为空时默认当前时间
     *
     * @param month  增加月份  正数相加、负数相减
     * @param date   指定时间
     * @param format 指定格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addMothToDate(int month, String date, String format) {
        Date newDate = new Date();
        if (null != date && !"".equals(date)) {
            newDate = string2Date(date, format);
        }

        return addMothToDate(month, newDate, format);
    }

    /**
     * 给指定日期增加天数,为空时默认当前时间
     *
     * @param day    增加天数 正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addDayToDate(int day, Date date, String format) {
        Calendar calendar = getCalendar(date, format);
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);

        calendar.add(Calendar.DATE, day);

        return sdf.format(calendar.getTime());
    }

    /**
     * 给指定日期增加天数,为空时默认当前时间
     *
     * @param day    增加天数 正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addDayToDate(int day, String date, String format) {
        Date newDate = new Date();
        if (null != date && !"".equals(date)) {
            newDate = string2Date(date, format);
        }

        return addDayToDate(day, newDate, format);
    }

    /**
     * 给指定日期增加小时,为空时默认当前时间
     *
     * @param hour   增加小时  正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addHourToDate(int hour, Date date, String format) {
        Calendar calendar = getCalendar(date, format);
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);

        calendar.add(Calendar.HOUR, hour);

        return sdf.format(calendar.getTime());
    }

    /**
     * 给指定日期增加小时,为空时默认当前时间
     *
     * @param hour   增加小时  正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addHourToDate(int hour, String date, String format) {
        Date newDate = new Date();
        if (null != date && !"".equals(date)) {
            newDate = string2Date(date, format);
        }

        return addHourToDate(hour, newDate, format);
    }

    /**
     * 给指定的日期增加分钟,为空时默认当前时间
     *
     * @param minute 增加分钟  正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addMinuteToDate(int minute, Date date, String format) {
        Calendar c=getCalendar(date, format);
        SimpleDateFormat df=new SimpleDateFormat(format);
        c.add(Calendar.DATE, minute);
        return df.format(c.getTime());
    }

    /**
     * 给指定的日期增加分钟,为空时默认当前时间
     *
     * @param minute 增加分钟  正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addMinuteToDate(int minute, String date, String format) {
        Date newDate = new Date();
        if (null != date && !"".equals(date)) {
            newDate = string2Date(date, format);
        }

        return addMinuteToDate(minute, newDate, format);
    }

    /**
     * 给指定日期增加秒,为空时默认当前时间
     *
     * @param second 增加秒 正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     */
    public static String addSecondToDate(int second, Date date, String format) {
        Calendar calendar = getCalendar(date, format);
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);

        calendar.add(Calendar.SECOND, second);

        return sdf.format(calendar.getTime());
    }

    /**
     * 给指定日期增加秒,为空时默认当前时间
     *
     * @param second 增加秒 正数相加、负数相减
     * @param date   指定日期
     * @param format 日期格式 为空默认 yyyy-mm-dd HH:mm:ss
     * @return String
     * @throws Exception
     */
    public static String addSecondToDate(int second, String date, String format) {
        Date newDate = new Date();
        if (null != date && !"".equals(date)) {
            newDate = string2Date(date, format);
        }

        return addSecondToDate(second, newDate, format);
    }

    /**
     * 获取指定格式指定时间的日历
     *
     * @param date   时间
     * @param format 格式
     * @return Calendar
     */
    public static Calendar getCalendar(Date date, String format) {
        if (date == null) {
            date = getCurrentDate(format);
        }

        Calendar calender = Calendar.getInstance();
        calender.setTime(date);

        return calender;
    }

    /**
     * 字符串转换为日期,日期格式为
     *
     * @param value
     * @return
     */
    public static Date string2Date(String value) {
        if (value == null || "".equals(value)) {
            return null;
        }

        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(SSHDateFormatUtils.DATE_FORMAT2);
        Date date = null;

        try {
            value = SSHDateFormatUtils.formatDate(value, SSHDateFormatUtils.DATE_FORMAT2);
            date = sdf.parse(value);
        } catch (Exception e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
        }
        return date;
    }


    public static Date stringToDate(String value) {
        if (value == null || "".equals(value)) {
            return null;
        }
        try {
            Date date = null;
            SimpleDateFormat sdf=null;
            value="20"+value;
            if (value.length() == 13){
                sdf = SSHDateFormatUtils.getFormat("yyyyMddHHmmss");
                value = SSHDateFormatUtils.formatDate(value,SSHDateFormatUtils.DATE_FORMAT2);
                date =   sdf.parse(value);
            }else if(value.length() == 14){
                date=  string2Date(value,SSHDateFormatUtils.TIME_NOFUll_FORMAT);
            }
            return date;
        } catch (Exception e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
            return null;
        }

    }

    public static String dateToString(String value) {
        if (value == null || "".equals(value)) {
            return null;
        }
        try {
            SimpleDateFormat sdf=null;
            value="20"+value;
            Date date =null;
            if (value.length() == 13){
                sdf = SSHDateFormatUtils.getFormat("yyyyMddHHmmss");
                value = SSHDateFormatUtils.formatDate(value,SSHDateFormatUtils.DATE_FORMAT2);
                date =   sdf.parse(value);
            }else if(value.length() == 14){
                sdf = SSHDateFormatUtils.getFormat("yyyyMMddHHmmss");
                SimpleDateFormat formatter = new SimpleDateFormat(SSHDateFormatUtils.DATE_FORMAT2);
                date = formatter.parse(value);
            }
            SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String time=formatter.format(date);
            return time;
        } catch (Exception e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
            return null;
        }

    }



    /**
     * 将字符串(格式符合规范)转换成Date
     *
     * @param value  需要转换的字符串
     * @param format 日期格式
     * @return Date
     */
    public static Date string2Date(String value, String format) {
        if (value == null || "".equals(value)) {
            return null;
        }
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);
        Date date = null;

        try {
            value = SSHDateFormatUtils.formatDate(value, format);
            date = sdf.parse(value);
        } catch (Exception e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
        }
        return date;
    }

    public static  Date stringToDate(String value, String format){
        if (value == null || "".equals(value)) {
            return null;
        }
        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);
        try{
            return sdf.parse(value);
        }catch(Exception e){
            e.printStackTrace();
        }
      return null;
    }


    /**
     * 将日期格式转换成String
     *
     * @param value  需要转换的日期
     * @param format 日期格式
     * @return String
     */
    public static String date2String(Date value, String format) {
        if (value == null) {
            return null;
        }

        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(format);
        return sdf.format(value);
    }

    /**
     * 日期转换为字符串
     *
     * @param value
     * @return
     */
    public static String date2String(Date value) {
        if (value == null) {
            return null;
        }

        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat(SSHDateFormatUtils.DATE_FORMAT2);
        return sdf.format(value);
    }

    /**
     * 获取指定日期的年份
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentYear(Date value) {
        String date = date2String(value, SSHDateFormatUtils.DATE_YEAR);
        return Integer.valueOf(date);
    }

    /**
     * 获取指定日期的年份
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentYear(String value) {
        Date date = string2Date(value, SSHDateFormatUtils.DATE_YEAR);
        Calendar calendar = getCalendar(date, SSHDateFormatUtils.DATE_YEAR);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取指定日期的月份
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentMonth(Date value) {
        String date = date2String(value, SSHDateFormatUtils.DATE_MONTH);
        return Integer.valueOf(date);
    }

    /**
     * 获取指定日期的月份
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentMonth(String value) {
        Date date = string2Date(value, SSHDateFormatUtils.DATE_MONTH);
        Calendar calendar = getCalendar(date, SSHDateFormatUtils.DATE_MONTH);

        return calendar.get(Calendar.MONTH);
    }

    /**
     * 获取指定日期的天份
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentDay(Date value) {
        String date = date2String(value, SSHDateFormatUtils.DATE_DAY);
        return Integer.valueOf(date);
    }

    /**
     * 获取指定日期的天份
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentDay(String value) {
        Date date = string2Date(value, SSHDateFormatUtils.DATE_DAY);
        Calendar calendar = getCalendar(date, SSHDateFormatUtils.DATE_DAY);

        return calendar.get(Calendar.DATE);
    }

    /**
     * 获取当前日期为星期几
     *
     * @param value 日期
     * @return String
     */
    public static String getCurrentWeek(Date value) {
        Calendar calendar = getCalendar(value, SSHDateFormatUtils.DATE_FORMAT1);
        int weekIndex = calendar.get(Calendar.DAY_OF_WEEK) - 1 < 0 ? 0 : calendar.get(Calendar.DAY_OF_WEEK) - 1;

        return weeks[weekIndex];
    }

    /**
     * 获取当前日期为星期几
     *
     * @param value 日期
     * @return String
     */
    public static String getCurrentWeek(String value) {
        Date date = string2Date(value, SSHDateFormatUtils.DATE_FORMAT1);
        return getCurrentWeek(date);
    }

    /**
     * 获取指定日期的小时
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentHour(Date value) {
        String date = date2String(value, SSHDateFormatUtils.DATE_HOUR);
        return Integer.valueOf(date);
    }

    /**
     * 获取指定日期的小时
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentHour(String value) {
        Date date = string2Date(value, SSHDateFormatUtils.DATE_HOUR);
        Calendar calendar = getCalendar(date, SSHDateFormatUtils.DATE_HOUR);

        return calendar.get(Calendar.DATE);
    }

    /**
     * 获取指定日期的分钟
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentMinute(Date value) {
        String date = date2String(value, SSHDateFormatUtils.DATE_MINUTE);
        return Integer.valueOf(date);
    }

    /**
     * 获取指定日期的分钟
     *
     * @param value 日期
     * @return int
     */
    public static int getCurrentMinute(String value) {
        Date date = string2Date(value, SSHDateFormatUtils.DATE_MINUTE);
        Calendar calendar = getCalendar(date, SSHDateFormatUtils.DATE_MINUTE);

        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 比较两个日期相隔多少天(月、年) <br>
     * 例:<br>
     * &nbsp;compareDate("2009-09-12", null, 0);//比较天 <br>
     * &nbsp;compareDate("2009-09-12", null, 1);//比较月 <br>
     * &nbsp;compareDate("2009-09-12", null, 2);//比较年 <br>
     *
     * @param startDay 需要比较的时间 不能为空(null),需要正确的日期格式 ,如:2009-09-12
     * @param endDay   被比较的时间  为空(null)则为当前时间
     * @param stype    返回值类型   0为多少天,1为多少个月,2为多少年
     * @return int
     */
    public static int compareDate(String startDay, String endDay, int stype) {
        int n = 0;
        startDay = SSHDateFormatUtils.formatDate(startDay, "yyyy-MM-dd");
        endDay = SSHDateFormatUtils.formatDate(endDay, "yyyy-MM-dd");

        String formatStyle = "yyyy-MM-dd";
        if (1 == stype) {
            formatStyle = "yyyy-MM";
        } else if (2 == stype) {
            formatStyle = "yyyy";
        }

        endDay = endDay == null ? getCurrentTime("yyyy-MM-dd") : endDay;

        DateFormat df = new SimpleDateFormat(formatStyle);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try {
            c1.setTime(df.parse(startDay));
            c2.setTime(df.parse(endDay));
        } catch (Exception e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
        }
        while (!c1.after(c2)) {                   // 循环对比,直到相等,n 就是所要的结果     
            n++;
            if (stype == 1) {
                c1.add(Calendar.MONTH, 1);          // 比较月份,月份+1     
            } else {
                c1.add(Calendar.DATE, 1);           // 比较天数,日期+1     
            }
        }
        n = n - 1;
        if (stype == 2) {
            n = (int) n / 365;
        }
        return n;
    }

    /**
     * 比较两个时间相差多少小时(分钟、秒)
     *
     * @param startTime 需要比较的时间 不能为空,且必须符合正确格式:2012-12-12 12:12:
     * @param endTime   需要被比较的时间 若为空则默认当前时间
     * @param type      1:小时   2:分钟   3:秒
     * @return int
     */
    public static int compareTime(String startTime, String endTime, int type) {
        //endTime是否为空,为空默认当前时间
        if (endTime == null || "".equals(endTime)) {
            endTime = getCurrentTime();
        }

        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat("");
        int value = 0;
        try {
            Date begin = sdf.parse(startTime);
            Date end = sdf.parse(endTime);
            long between = (end.getTime() - begin.getTime()) / 1000;  //除以1000转换成秒
            if (type == 1) {   //小时
                value = (int) (between / 3600);
            } else if (type == 2) {
                value = (int) (between  / 60);
            } else if (type == 3) {
                value = (int) (between);
            }
        } catch (ParseException e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
        }
        return value;
    }

    /**
     * 比较两个时间相差多少小时(分钟、秒)
     *
     * @param begin 需要比较的时间 不能为空,且必须符合正确格式:2012-12-12 12:12:
     * @param end   需要被比较的时间 若为空则默认当前时间
     * @param type      1:小时   2:分钟   3:秒
     * @return int
     */
    public static int compareTime(Date begin, Date end, int type) {
        //endTime是否为空,为空默认当前时间
        if (end == null ) {
            end = new Date();
        }

        SimpleDateFormat sdf = SSHDateFormatUtils.getFormat("");
        int value = 0;
        try {
            long between = (end.getTime() - begin.getTime()) / 1000;  //除以1000转换成豪秒
            if (type == 1) {   //小时
                value = (int) (between % (24 * 36000) / 3600);
            } else if (type == 2) {
                value = (int) (between % 3600 / 60);
            } else if (type == 3) {
                value = (int) (between % 60 / 60);
            }
        } catch (Exception e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
        }
        return value;
    }
    /**
     * 比较两个日期的大小。<br>
     * 若date1 > date2 则返回 1<br>
     * 若date1 = date2 则返回 0<br>
     * 若date1 < date2 则返回-1
     *
     * @param date1
     * @param date2
     * @param format 待转换的格式
     * @return 比较结果
     */
    public static int compare(String date1, String date2, String format) {
        DateFormat df = SSHDateFormatUtils.getFormat(format);
        try {
            Date dt1 = df.parse(date1);
            Date dt2 = df.parse(date2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取指定月份的第一天
     *
     * @param date
     * @return
     */
    public static String getMonthFirstDate(String date) {
        date = SSHDateFormatUtils.formatDate(date);
        return SSHDateFormatUtils.formatDate(date, "yyyy-MM") + "-01";
    }

    /**
     * 获取指定月份的最后一天
     *
     * @param date
     * @return
     */
    public static String getMonthLastDate(String date) {
        Date strDate = SSHDateUtils.string2Date(getMonthFirstDate(date));
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(strDate);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        return SSHDateFormatUtils.formatDate(calendar.getTime());
    }

    /**
     * 获取所在星期的第一天
     *
     * @param date
     * @return
     */
    @SuppressWarnings("static-access")
    public static Date getWeekFirstDate(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        int today = now.get(Calendar.DAY_OF_WEEK);
        int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
        now.set(now.DATE, first_day_of_week);
        return now.getTime();
    }

    /**
     * 获取所在星期的最后一天
     *
     * @param date
     * @return
     */
    @SuppressWarnings("static-access")
    public static Date geWeektLastDate(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        int today = now.get(Calendar.DAY_OF_WEEK);
        int first_day_of_week = now.get(Calendar.DATE) + 2 - today; // 星期一
        int last_day_of_week = first_day_of_week + 6; // 星期日
        now.set(now.DATE, last_day_of_week);
        return now.getTime();
    }

    public  static  Date timeToDate(String value){
        if (value != null && !"".equals(value)) {
            try {
                long stamp=Long.valueOf(value);
                Timestamp   timestamp=new Timestamp(stamp);
                String timeStr=timestamp.toString().substring(0, timestamp.toString().indexOf("."));
                SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss" );
                Date date = string2Date(timeStr);
                return date;
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public  static  Date timeToDate(Long value){
        if (value != null ) {
            try {
                Timestamp   timestamp=new Timestamp(value);
                String timeStr=timestamp.toString().substring(0, timestamp.toString().indexOf("."));
                SimpleDateFormat sdf =   new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss" );
                Date date = string2Date(timeStr);
                return date;
            } catch (Exception var4) {
                var4.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public  static  Date StringToDate(String date){
        SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return sdf2.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();// todo 去掉e.printStackTrace();  使用 LOGGER.error("msg",e)
            return null;
        }
    }

    /***
     * 获取当前时间的前一天
     * @return
     */
    public static Date getDayBefore(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        date = calendar.getTime();
        return date;
    }

    /***
     * 获取当前时间前7天
     * @return
     */
    public static Date getWeekData(){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        date = calendar.getTime();
        return date;
    }
    /**
     *当前时间
     */
    public static Date getNowTime() throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowdayTime = dateFormat.format(new Date());
        Date nowDate = dateFormat.parse(nowdayTime);
        return nowDate;
    }
    /**
     * 获取当前日期的 0点0分0秒
     * */
    public static Date getZeroTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();
        return zero;
    }

    /**
     * 获取当前日期的 23点59分59秒
     * */
    public static Date getTTime(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date zero = calendar.getTime();
        return zero;
    }

    /**
     * 获取传入时间的前一天数据
     * */
    public static Date getDayBefore(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    /**
     * 获取传入时间的后一天数据
     * */
    public static Date getDayAfter(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    /**
     * 计算两个日期之间相差的天数
     * @param startDate 开始时间
     * @param endDate  结束时间
     * @return 相差天数
     * @throws ParseException
     */
    public static int daysBetween(Date startDate,Date endDate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        startDate=sdf.parse(sdf.format(startDate));
        endDate=sdf.parse(sdf.format(endDate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endDate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);

        return Integer.parseInt(String.valueOf(between_days));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值