乐趣区

关于java:ssm工具类

日期工具类


public class DateUtil {

    // == 格局到年 ==
    /**
     * 日期格局,年份,例如:2004,2008
     */
    public static final String DATE_FORMAT_YYYY = "yyyy";


    // == 格局到年月 ==
    /**
     * 日期格局,年份和月份,例如:200707,200808
     */
    public static final String DATE_FORMAT_YYYYMM = "yyyyMM";

    /**
     * 日期格局,年份和月份,例如:200707,2008-08
     */
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";


    // == 格局到年月日 ==
    /**
     * 日期格局,年月日,例如:050630,080808
     */
    public static final String DATE_FORMAT_YYMMDD = "yyMMdd";

    /**
     * 日期格局,年月日,用横杠离开,例如:06-12-25,08-08-08
     */
    public static final String DATE_FORMAT_YY_MM_DD = "yy-MM-dd";

    /**
     * 日期格局,年月日,例如:20050630,20080808
     */
    public static final String DATE_FORMAT_YYYYMMDD = "yyyyMMdd";

    /**
     * 日期格局,年月日,用横杠离开,例如:2006-12-25,2008-08-08
     */
    public static final String DATE_FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

    /**
     * 日期格局,年月日,用横杠离开,例如:2020/07/16
     */
    public static final String DATE_FORMAT_YYYY_MM_DD_2 = "yyyy/MM/dd";

    /**
     * 日期格局,年月日,例如:2016.10.05
     */
    public static final String DATE_FORMAT_POINTYYYYMMDD = "yyyy.MM.dd";

    /**
     * 日期格局,年月日,例如:2016 年 10 月 05 日
     */
    public static final String DATE_TIME_FORMAT_YYYY 年 MM 月 DD 日 = "yyyy 年 MM 月 dd 日";


    // == 格局到年月日 时候 ==

    /**
     * 日期格局,年月日时分,例如:200506301210,200808081210
     */
    public static final String DATE_FORMAT_YYYYMMDDHHmm = "yyyyMMddHHmm";

    /**
     * 日期格局,年月日时分,例如:20001230 12:00,20080808 20:08
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDD_HH_MI = "yyyyMMdd HH:mm";

    /**
     * 日期格局,年月日时分,例如:2000-12-30 12:00,2008-08-08 20:08
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI = "yyyy-MM-dd HH:mm";

    /**
     * 日期格局,年月日时分,例如:2020/07/16 15:12
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_2 = "yyyy/MM/dd HH:mm";

    // == 格局到年月日 时分秒 ==
    /**
     * 日期格局,年月日时分秒,例如:20001230120000,20080808200808
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISS = "yyyyMMddHHmmss";

    /**
     * 日期格局,年月日时分秒,年月日用横杠离开,时分秒用冒号离开
     * 例如:2005-05-10 23:20:00,2008-08-08 20:08:08
     */
    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS_2 = "yyyy/MM/dd HH:mm:ss";


    // == 格局到年月日 时分秒 毫秒 ==
    /**
     * 日期格局,年月日时分秒毫秒,例如:20001230120000123,20080808200808456
     */
    public static final String DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS = "yyyyMMddHHmmssSSS";


    // == 非凡格局 ==
    /**
     * 日期格局,月日时候,例如:10-05 12:00
     */
    public static final String DATE_FORMAT_MMDDHHMI = "MM-dd HH:mm";


    /* ************ 工具办法 ***************   */

    /**
     * 获取某日期的年份
     * @param date
     * @return
     */
    public static Integer getYear(Date date) {Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.YEAR);
    }

    /**
     * 获取某日期的月份
     * @param date
     * @return
     */
    public static Integer getMonth(Date date) {Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取某日期的日数
     * @param date
     * @return
     */
    public static Integer getDay(Date date){Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int day=cal.get(Calendar.DATE);// 获取日
        return day;
    }

    /**
     * 获取以后日期是星期几
     * @param date
     * @return 以后日期是星期几
     */
    public static String getWeekOfDate(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];
    }

    /**
     * 格式化 Date 工夫
     * @param time Date 类型工夫
     * @param timeFromat String 类型格局
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat){DateFormat dateFormat=new SimpleDateFormat(timeFromat);
        return dateFormat.format(time);
    }

    /**
     * 格式化 Timestamp 工夫
     * @param timestamp Timestamp 类型工夫
     * @param timeFromat
     * @return 格式化后的字符串
     */
    public static String parseTimestampToStr(Timestamp timestamp,String timeFromat){SimpleDateFormat df = new SimpleDateFormat(timeFromat);
        return df.format(timestamp);
    }

    /**
     * 格式化 Date 工夫
     * @param time Date 类型工夫
     * @param timeFromat String 类型格局
     * @param defaultValue 默认值为以后工夫 Date
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat, final Date defaultValue){
        try{DateFormat dateFormat=new SimpleDateFormat(timeFromat);
            return dateFormat.format(time);
        }catch (Exception e){if(defaultValue!=null)
                return parseDateToStr(defaultValue, timeFromat);
            else
                return parseDateToStr(new Date(), timeFromat);
        }
    }

    /**
     * 格式化 Date 工夫
     * @param time Date 类型工夫
     * @param timeFromat String 类型格局
     * @param defaultValue 默认工夫值 String 类型
     * @return 格式化后的字符串
     */
    public static String parseDateToStr(Date time, String timeFromat, final String defaultValue){
        try{DateFormat dateFormat=new SimpleDateFormat(timeFromat);
            return dateFormat.format(time);
        }catch (Exception e){return defaultValue;}
    }

    /**
     * 格式化 String 工夫
     * @param time String 类型工夫
     * @param timeFromat String 类型格局
     * @return 格式化后的 Date 日期
     */
    public static Date parseStrToDate(String time, String timeFromat) {if (time == null || time.equals("")) {return null;}

        Date date=null;
        try{DateFormat dateFormat=new SimpleDateFormat(timeFromat);
            date=dateFormat.parse(time);
        }catch(Exception e){ }
        return date;
    }

    /**
     * 格式化 String 工夫
     * @param strTime String 类型工夫
     * @param timeFromat String 类型格局
     * @param defaultValue 异样时返回的默认值
     * @return
     */
    public static Date parseStrToDate(String strTime, String timeFromat,
                                      Date defaultValue) {
        try {DateFormat dateFormat = new SimpleDateFormat(timeFromat);
            return dateFormat.parse(strTime);
        } catch (Exception e) {return defaultValue;}
    }

    /**
     * 当 strTime 为 2008- 9 时返回为 2008-9-1 00:00 格局日期工夫,无奈转换返回 null.
     * @param strTime
     * @return
     */
    public static Date strToDate(String strTime) {if(strTime==null || strTime.trim().length()<=0)
            return null;

        Date date = null;
        List<String> list = new ArrayList<String>(0);

        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS_2);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_2);
        list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        list.add(DATE_FORMAT_YYYY_MM_DD);
        list.add(DATE_FORMAT_YYYY_MM_DD_2);
        list.add(DATE_FORMAT_YYYYMMDD);
        list.add(DATE_FORMAT_YYYY_MM);
        list.add(DATE_FORMAT_YYYYMM);
        list.add(DATE_FORMAT_YYYY);


        for (Iterator iter = list.iterator(); iter.hasNext();) {String format = (String) iter.next();
            if(strTime.indexOf("-")>0 && format.indexOf("-")<0)
                continue;
            if(strTime.indexOf("-")<0 && format.indexOf("-")>0)
                continue;
            if(strTime.length()>format.length())
                continue;
            date = parseStrToDate(strTime, format);
            if (date != null)
                break;
        }

        return date;
    }

    /**
     * 解析两个日期之间的所有月份
     * @param beginDateStr 开始日期,至多准确到 yyyy-MM
     * @param endDateStr 完结日期,至多准确到 yyyy-MM
     * @return yyyy-MM 日期汇合
     */
    public static List<String> getMonthListOfDate(String beginDateStr, String endDateStr) {
        // 指定要解析的工夫格局
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
        // 返回的月份列表
        String sRet = "";

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        GregorianCalendar beginGC = null;
        GregorianCalendar endGC = null;
        List<String> list = new ArrayList<String>();

        try {
            // 将字符串 parse 成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = new GregorianCalendar();
            beginGC.setTime(beginDate);

            endGC = new GregorianCalendar();
            endGC.setTime(endDate);

            // 直到两个工夫雷同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {sRet = beginGC.get(Calendar.YEAR) + "-"
                        + (beginGC.get(Calendar.MONTH) + 1);
                list.add(sRet);
                // 以月为单位,减少工夫
                beginGC.add(Calendar.MONTH, 1);
            }
            return list;
        } catch (Exception e) {e.printStackTrace();
            return null;
        }
    }

    /**
     * 解析两个日期段之间的所有日期
     * @param beginDateStr 开始日期,至多准确到 yyyy-MM-dd
     * @param endDateStr 完结日期,至多准确到 yyyy-MM-dd
     * @return yyyy-MM-dd 日期汇合
     */
    public static List<String> getDayListOfDate(String beginDateStr, String endDateStr) {
        // 指定要解析的工夫格局
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");

        // 定义一些变量
        Date beginDate = null;
        Date endDate = null;

        Calendar beginGC = null;
        Calendar endGC = null;
        List<String> list = new ArrayList<String>();

        try {
            // 将字符串 parse 成日期
            beginDate = f.parse(beginDateStr);
            endDate = f.parse(endDateStr);

            // 设置日历
            beginGC = Calendar.getInstance();
            beginGC.setTime(beginDate);

            endGC = Calendar.getInstance();
            endGC.setTime(endDate);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            // 直到两个工夫雷同
            while (beginGC.getTime().compareTo(endGC.getTime()) <= 0) {list.add(sdf.format(beginGC.getTime()));
                // 以日为单位,减少工夫
                beginGC.add(Calendar.DAY_OF_MONTH, 1);
            }
            return list;
        } catch (Exception e) {e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取当下年份指定前后数量的年份汇合
     * @param before 当下年份前年数
     * @param behind 当下年份后年数
     * @return 汇合
     */
    public static List<Integer> getYearListOfYears(int before,int behind) {if (before<0 || behind<0) {return null;}
        List<Integer> list = new ArrayList<Integer>();
        Calendar c = null;
        c = Calendar.getInstance();
        c.setTime(new Date());
        int currYear = Calendar.getInstance().get(Calendar.YEAR);

        int startYear = currYear - before;
        int endYear = currYear + behind;
        for (int i = startYear; i < endYear; i++) {list.add(Integer.valueOf(i));
        }
        return list;
    }

    /**
     * 获取以后日期是一年中第几周
     * @param date
     * @return
     */
    public static Integer getWeekthOfYear(Date date) {Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setMinimalDaysInFirstWeek(7);
        c.setTime(date);

        return c.get(Calendar.WEEK_OF_YEAR);
    }

    /**
     * 获取某一年各星期的始终工夫
     * 实例:getWeekList(2016),第 52 周 (从 2016-12-26 至 2017-01-01)
     * @param year 年份
     * @return
     */
    public static HashMap<Integer,String> getWeekTimeOfYear(int year) {HashMap<Integer,String> map = new LinkedHashMap<Integer,String>();
        Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        int count = getWeekthOfYear(c.getTime());

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dayOfWeekStart = "";
        String dayOfWeekEnd = "";
        for (int i = 1; i <= count; i++) {dayOfWeekStart = sdf.format(getFirstDayOfWeek(year, i));
            dayOfWeekEnd = sdf.format(getLastDayOfWeek(year, i));
            map.put(Integer.valueOf(i), "第"+i+"周 (从"+dayOfWeekStart + "至" + dayOfWeekEnd+")");
        }
        return map;

    }

    /**
     * 获取某一年的总周数
     * @param year
     * @return
     */
    public static Integer getWeekCountOfYear(int year){Calendar c = new GregorianCalendar();
        c.set(year, Calendar.DECEMBER, 31, 23, 59, 59);
        int count = getWeekthOfYear(c.getTime());
        return count;
    }

    /**
     * 获取指定日期所在周的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfWeek(Date date) {Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        return c.getTime();}

    /**
     * 获取指定日期所在周的最初一天
     * @param date
     * @return
     */
    public static Date getLastDayOfWeek(Date date) {Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        return c.getTime();}

    /**
     * 获取某年某周的第一天
     * @param year 指标年份
     * @param week 指标周数
     * @return
     */
    public static Date getFirstDayOfWeek(int year, int week) {Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getFirstDayOfWeek(cal.getTime());
    }

    /**
     * 获取某年某周的最初一天
     * @param year 指标年份
     * @param week 指标周数
     * @return
     */
    public static Date getLastDayOfWeek(int year, int week) {Calendar c = new GregorianCalendar();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, Calendar.JANUARY);
        c.set(Calendar.DATE, 1);

        Calendar cal = (GregorianCalendar) c.clone();
        cal.add(Calendar.DATE, week * 7);

        return getLastDayOfWeek(cal.getTime());
    }

    /**
     * 获取某年某月的第一天
     * @param year 指标年份
     * @param month 指标月份
     * @return
     */
    public static Date getFirstDayOfMonth(int year,int month){
        month = month-1;
        Calendar   c   =   Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);

        int day = c.getActualMinimum(c.DAY_OF_MONTH);

        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();}

    /**
     * 获取某年某月的最初一天
     * @param year 指标年份
     * @param month 指标月份
     * @return
     */
    public static Date getLastDayOfMonth(int year,int month){
        month = month-1;
        Calendar   c   =   Calendar.getInstance();
        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month);
        int day = c.getActualMaximum(c.DAY_OF_MONTH);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();}

    /**
     * 获取某个日期为星期几
     * @param date
     * @return String "星期 *"
     */
    public static String getDayWeekOfDate1(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];
    }

    /**
     * 取得指定日期的星期几数
     * @param date
     * @return int
     */
    public static Integer getDayWeekOfDate2(Date date){Calendar aCalendar = Calendar.getInstance();
        aCalendar.setTime(date);
        int weekDay = aCalendar.get(Calendar.DAY_OF_WEEK);
        return weekDay;
    }

    /**
     * 验证字符串是否为日期
     * 验证格局:YYYYMMDD、YYYY_MM_DD、YYYYMMDDHHMISS、YYYYMMDD_HH_MI、YYYY_MM_DD_HH_MI、YYYYMMDDHHMISSSSS、YYYY_MM_DD_HH_MI_SS
     * @param strTime
     * @return null 时返回 false;true 为日期,false 不为日期
     */
    public static boolean validateIsDate(String strTime) {if (strTime == null || strTime.trim().length() <= 0)
            return false;

        Date date = null;
        List<String> list = new ArrayList<String>(0);

        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISSSSS);
        list.add(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYYMMDD_HH_MI);
        list.add(DATE_TIME_FORMAT_YYYYMMDDHHMISS);
        list.add(DATE_FORMAT_YYYY_MM_DD);
        //list.add(DATE_FORMAT_YY_MM_DD);
        list.add(DATE_FORMAT_YYYYMMDD);
        //list.add(DATE_FORMAT_YYYY_MM);
        //list.add(DATE_FORMAT_YYYYMM);
        //list.add(DATE_FORMAT_YYYY);

        for (Iterator iter = list.iterator(); iter.hasNext();) {String format = (String) iter.next();
            if(strTime.indexOf("-")>0 && format.indexOf("-")<0)
                continue;
            if(strTime.indexOf("-")<0 && format.indexOf("-")>0)
                continue;
            if(strTime.length()>format.length())
                continue;
            date = parseStrToDate(strTime.trim(), format);
            if (date != null)
                break;
        }

        if (date != null) {System.out.println("生成的日期:"+DateUtil.parseDateToStr(date, DateUtil.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS, "--null--"));
            return true;
        }
        return false;
    }

    /**
     * 将指定日期的时分秒格局为零
     * @param date
     * @return
     */
    public static Date formatHhMmSsOfDate(Date date) {Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();}

    /**
     * 取得指定工夫加减参数后的日期 (不计算则输出 0)
     * @param date 指定日期
     * @param year 年数,可正可负
     * @param month 月数,可正可负
     * @param day 天数,可正可负
     * @param hour 小时数,可正可负
     * @param minute 分钟数,可正可负
     * @param second 秒数,可正可负
     * @param millisecond 毫秒数,可正可负
     * @return 计算后的日期
     */
    public static Date addDate(Date date,int year,int month,int day,int hour,int minute,int second,int millisecond){Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.YEAR, year);// 加减年数
        c.add(Calendar.MONTH, month);// 加减月数
        c.add(Calendar.DATE, day);// 加减天数
        c.add(Calendar.HOUR,hour);// 加减小时数
        c.add(Calendar.MINUTE, minute);// 加减分钟数
        c.add(Calendar.SECOND, second);// 加减秒
        c.add(Calendar.MILLISECOND, millisecond);// 加减毫秒数

        return c.getTime();}

    /**
     * 取得两个日期的工夫戳之差
     * @param startDate
     * @param endDate
     * @return
     */
    public static Long getDistanceTimestamp(Date startDate,Date endDate){long daysBetween=(endDate.getTime()-startDate.getTime()+1000000)/(3600*24*1000);
        return daysBetween;
    }

    /**
     * 判断二个工夫是否为同年同月
     * @param date1
     * @param date2
     * @return
     */
    public static Boolean compareIsSameMonth(Date date1,Date date2){
        boolean flag = false;
        int year1  = getYear(date1);
        int year2 = getYear(date2);
        if(year1 == year2){int month1 = getMonth(date1);
            int month2 = getMonth(date2);
            if(month1 == month2)flag = true;
        }
        return flag;
    }

    /**
     * 取得两个工夫相差间隔多少天多少小时多少分多少秒
     * @param one 工夫参数 1 格局:1990-01-01 12:00:00
     * @param two 工夫参数 2 格局:2009-01-01 12:00:00
     * @return long[] 返回值为:{ 天, 时, 分, 秒}
     */
    public static long[] getDistanceTime(Date one, Date two) {
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1<time2) {diff = time2 - time1;} else {diff = time1 - time2;}
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        } catch (Exception e) {e.printStackTrace();
        }
        long[] times = {day, hour, min, sec};
        return times;
    }

    /**
     * 两个工夫相差间隔多少天多少小时多少分多少秒
     * @param str1 工夫参数 1 格局:1990-01-01 12:00:00
     * @param str2 工夫参数 2 格局:2009-01-01 12:00:00
     * @return String 返回值为:{天, 时, 分, 秒}
     */
    public static long[] getDistanceTime(String str1, String str2) {DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1<time2) {diff = time2 - time1;} else {diff = time1 - time2;}
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);
        } catch (ParseException e) {e.printStackTrace();
        }
        long[] times = {day, hour, min, sec};
        return times;
    }

    /**
     * 两个工夫之间相差间隔多少天
     * @param str1 工夫参数 1:* @param str2 工夫参数 2:* @return 相差天数
     */
    public static Long getDistanceDays(String str1, String str2) throws Exception{DateFormat df = new SimpleDateFormat(DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date one;
        Date two;
        long days=0;
        try {one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff ;
            if(time1<time2) {diff = time2 - time1;} else {diff = time1 - time2;}
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {e.printStackTrace();
        }
        return days;
    }

    /**
     * 获取指定工夫的那天 00:00:00.000 的工夫
     * @param date
     * @return
     */
    public static Date getDayBeginTime(final Date date) {Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();}

    /**
     * 获取指定工夫的那天 23:59:59.999 的工夫
     * @param date
     * @return
     */
    public static Date getDayEndTime(final Date date) {Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 23);
        c.set(Calendar.MINUTE, 59);
        c.set(Calendar.SECOND, 59);
        c.set(Calendar.MILLISECOND, 999);
        return c.getTime();}

    /**
     * 零碎以后的工夫戳
     *
     * @return 零碎以后的工夫戳
     */
    public static Timestamp getTimestamp() {return new Timestamp(System.currentTimeMillis());
    }

    public static void main(String [] args){
        try {DateUtil dateUtil = new DateUtil();
            System.out.println();
            Calendar rightNow = Calendar.getInstance();
            int hour = rightNow.get(Calendar.HOUR_OF_DAY);// 时
            int minute = rightNow.get(Calendar.MINUTE);// 分
            System.out.println(hour+":"+minute);

            PasswordEncodeDecode p=new PasswordEncodeDecode();
            String pass = p.passwordEncode("83abe08f8b1040b2a75b6faf29384cfd","#XJYTghsjzx2021");// 数据库明码加密
            System.out.println("ds----》" + pass);
        } catch (Exception e) {// TODO: handle exception}

    }

    // 获取以后工夫戳 格局为 yyyyMMddHHmmss
    public static String getNowDate() {
        // 失去 long 类型以后工夫
        long  l = System.currentTimeMillis();
        //new 日期对象
        Date date =  new  Date(l);
        // 转换提日期输入格局
        SimpleDateFormat dateFormat =  new SimpleDateFormat("yyyyMMddHHmmss");
        return dateFormat.format(date);
    }


}

响应工具类(接口返回数据格式)


@Data
@ApiModel(value="接口返回对象", description="接口返回对象")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 胜利标记
     */
    @ApiModelProperty(value = "胜利标记")
    private boolean success = true;

    /**
     * 返回解决音讯
     */
    @ApiModelProperty(value = "返回解决音讯")
    private String message = "操作胜利!";

    /**
     * 返回代码
     */
    @ApiModelProperty(value = "返回代码")
    private Integer code = 0;
    
    /**
     * 返回数据对象 data
     */
    @ApiModelProperty(value = "返回数据对象")
    private T result;
    
    /**
     * 工夫戳
     */
    @ApiModelProperty(value = "工夫戳")
    private long timestamp = System.currentTimeMillis();

    public Result() {}
    
    public Result<T> success(String message) {
        this.message = message;
        this.code = CommonConstant.SC_OK_200;
        this.success = true;
        return this;
    }
    
    
    public static Result<Object> ok() {Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage("胜利");
        return r;
    }
    
    public static Result<Object> ok(String msg) {Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        return r;
    }
    
    public static Result<Object> ok(Object data) {Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setResult(data);
        return r;
    }

    public static Result<Object> ok(Object data, String msg) {Result<Object> r = new Result<Object>();
        r.setSuccess(true);
        r.setCode(CommonConstant.SC_OK_200);
        r.setMessage(msg);
        r.setResult(data);
        return r;
    }

    public static Result<Object> error(String msg) {return error(CommonConstant.SC_INTERNAL_SERVER_ERROR_500, msg);
    }
    
    public static Result<Object> error(int code, String msg) {Result<Object> r = new Result<Object>();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public Result<T> error500(String message) {
        this.message = message;
        this.code = CommonConstant.SC_INTERNAL_SERVER_ERROR_500;
        this.success = false;
        return this;
    }
    /**
     * 无权限拜访返回后果
     */
    public static Result<Object> noauth(String msg) {return error(CommonConstant.SC_NO_AUTHZ, msg);
    }
}
退出移动版