日期处理工具类

全部是代码

1.日期格式规范

 public static final String PATTERNSTR_YMD    ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)"; //"yyyy-MM-dd" 验证
    public static final String PATTERNSTR_YMD_H  ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29 (0\\d{1}|1\\d{1}|2[0-3]))"; //"yyyy-MM-dd hh24" 验证
    public static final String PATTERNSTR_YMD_HM ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29  (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)"; //"yyyy-MM-dd hh24:mm" 验证
    public static final String PATTERNSTR_YMD_HMS="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29 (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)"; //"yyyy-MM-dd hh24:mm:ss"


    public static final String PATTERNSTR_YMD2     ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29)"; //"yyyy-MM-dd" 验证
    public static final String PATTERNSTR_YMD_H2   ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29 (0\\d{1}|1\\d{1}|2[0-3]))"; //"yyyy/MM/dd hh24" 验证
    public static final String PATTERNSTR_YMD_HM2  ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29  (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)"; //"yyyy/MM/dd hh24:mm" 验证
    public static final String PATTERNSTR_YMD_HMS2 ="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/(((0[13578]|1[02])/(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)/(0[1-9]|[12][0-9]|30))|(02/(0[1-9]|[1][0-9]|2[0-8]))) (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))/02/29  (0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)"; //"yyyy/MM/dd hh24:mm:ss"

    public static final String PATTERNSTR_YM="(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-((0[1-9])|(1[0-2])))";
    public static final String PATTERNSTR_Y="([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})";

    public static final String PATTERNSTR_HMS="((0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1}):([0-5]\\d{1})$)"; //"hh24:mm:ss"
    public static final String PATTERNSTR_HM="((0\\d{1}|1\\d{1}|2[0-3]):([0-5]\\d{1})$)"; //"hh24:mm"
    public static final String PATTERNSTR_H="((0\\d{1}|1\\d{1}|2[0-3])$)"; //"hh24:mm"
    public static final String DateMode_1 = "yyyy-MM-dd";
    public static final String DateMode_2 = "yyyy-MM-dd HH:mm";
    public static final String DateMode_3 = "yyyyMMddHHmmssSSS";
    public static final String DateMode_4 = "yyyy-MM-dd HH:mm:ss";
    public static final String DateMode_5 = "HHmmssSS";
    public static final String DateMode_6 = "yyyyMMdd";
    public static final String DateMode_7 = "HHmmss";
    public static final String DateMode_8 = "yyyy";
    public static final String DateMode_9 = "yyyyMMddHHmmss";
    public static final String DateMode_10 = "HH:mm:ss";
    public static final String DateMode_11 = "yyyy年MM月dd日HH:mm:ss";
    public static final String DateMode_12 = "MM-dd";
    public static final SimpleDateFormat date_format         = new SimpleDateFormat(DateMode_1);
    public static final SimpleDateFormat date_format_str     = new SimpleDateFormat(DateMode_6);
    public static final SimpleDateFormat date_format_allstr  = new SimpleDateFormat(DateMode_9);
    public static final SimpleDateFormat datetime_format     = new SimpleDateFormat(DateMode_4);
    public static final SimpleDateFormat now_time            = new SimpleDateFormat(DateMode_10);
    public static final  String          DATE_REGEX          = "\\d{4}-\\d{1,2}-\\d{1,2}";
    public static final  String          DEFAULT_DATE_FORMAT = DateMode_1;

2. 格式化制定的date

 /**
     * 格式化制定的date
     * @param date
     * @param simpleDateFormat
     * @return
     */
    public static String formateDate(Date date, SimpleDateFormat simpleDateFormat) {
        if (date == null) {
            return null;
        }
        if(simpleDateFormat==null){
            return null;
        }
        return simpleDateFormat.format(date);
    }

    /**
     * 格式化制定的date
     * @param date
     * @param simpleDateFormatStr
     * @return
     */
    public static String formateDate(Date date,String simpleDateFormatStr) {
        if (date == null) {
            return null;
        }
        if(simpleDateFormatStr==null){
            simpleDateFormatStr =DEFAULT_DATE_FORMAT;
        }
        return formateDate(date,new SimpleDateFormat(simpleDateFormatStr));
    }

3.获取某天的最小时间


    /**
     * 获取某天的最小时间
     * @param date
     * @return
     */
    public static Date getDateMin(Date date) {
        if(date==null) {
            return date;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 1);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }

4.获取某天的最大时间

 /**
     * 获取某天的最大时间
     * @param date
     * @return
     */
    public static Date getDateMax(Date date) {
        if(date==null) {
            return 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();
    }

5.获取某天的最大时间插入数据库时

 /**
     * 获取某天的最大时间插入数据库时
     * @param date
     * @return
     */
    public static Date getDateMaxDB(Date date) {
        if(date==null) {
            return 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, 0);
        return c.getTime();
    }

6.startDate加上days是否小于endate,超过返回true,否则返回false

 /**
     * startDate加上days是否小于endate,超过返回true,否则返回false
     * @param startDate
     * @param endDate
     * @param days
     * @return
     */
    public static boolean compareDate(Date startDate, Date endDate, int days) {
        if (startDate == null || endDate == null) {
            return false;
        }
        return addDays(startDate, days).before(endDate);
    }

7. startDate加上days是否等于endDate,满足返回true,否则返回false

 /**
     * startDate加上days是否等于endDate,满足返回true,否则返回false
     * @param startDate
     * @param endDate
     * @param days
     * @return
     */
    public static boolean compareDateWithEqual(Date startDate, Date endDate, int days) {
        if (startDate == null || endDate == null) {
            return false;
        }
        Date d = addDays(startDate, days);
        int r = d.compareTo(endDate);
        if (r <= 0) {
            return true;
        }
        return false;
    }

8.获取一周中的第几天

 /**
     * 获取一周中的第几天
     * @param date
     * @return
     */
    public static int getWeekByDate(Date date) {
        Calendar c = Calendar.getInstance();
        if (date != null) {
            c.setTime(date);
        }
        return c.get(Calendar.DAY_OF_WEEK);
    }

9.获取相差几天

 /**
     * 获取相差几天
     * @param sd
     * @param ed
     * @return
     */
    public static int getIntervalDayCount(Date sd, Date ed) {
        return (int) ((ed.getTime() - sd.getTime()) / (3600 * 24 * 1000) + 1);
    }

10.返回两个时间之间的天数

 /**
     * 返回两个时间之间的天数
     * @param startDay
     * @param endDay
     * @return
     */
    public static Long getDaysBetweenTwoDays(Date startDay, Date endDay) {
        return (endDay.getTime() - startDay.getTime()) / (1000 * 60 * 60 * 24);
    }

11.获取某个时间段的时间信息

/**
     * 获取某个时间段的时间信息
     * @param dBegin
     * @param dEnd
     * @return
     */
    public static List<Date> findDates(Date dBegin, Date dEnd) {
        List lDate = new ArrayList();
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后

        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则,为给定的日历字段添加或减去指定的时间量
            lDate.add(calBegin.getTime());
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
        }
        return lDate;
    }

 public static Date getDate2(String dateStr) throws ParseException{
        Date date =null;
        if(Pattern.matches(DateUtil.PATTERNSTR_YMD, dateStr)){//yyyy-MM-dd
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
            date=sf1.parse(dateStr);
        }else if(Pattern.matches(DateUtil.PATTERNSTR_YMD2, dateStr)){//yyyy-MM
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd");
            date=sf1.parse(dateStr);
        }else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_H, dateStr)){//yyyy
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH");
            date=sf1.parse(dateStr);
        }else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_H2, dateStr)){//yyyy
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd HH");
            date=sf1.parse(dateStr);
        }else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HM, dateStr)){//yyyy
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            date=sf1.parse(dateStr);
        }else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HM2, dateStr)){//yyyy
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
            date=sf1.parse(dateStr);
        }else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HMS, dateStr)){//yyyy
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            date=sf1.parse(dateStr);
        }else if(Pattern.matches(DateUtil.PATTERNSTR_YMD_HMS2, dateStr)){//yyyy
            SimpleDateFormat sf1 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            date=sf1.parse(dateStr);
        }
        return date;
    }

12.验证是否时间

  /**
     * 验证是否时间
     * @param sDate
     * @return
     */
    public static boolean isValidDate(String sDate) {
        String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
        String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
                + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
                + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
                + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        if ((sDate != null)) {
            Pattern pattern = Pattern.compile(datePattern1);
            Matcher match = pattern.matcher(sDate);
            if (match.matches()) {
                pattern = Pattern.compile(datePattern2);
                match = pattern.matcher(sDate);
                return match.matches();
            } else {
                return false;
            }
        }
        return false;
    }

13.获取某个时间所在月的最后一天

 /**
     *获取某个时间所在月的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfMonth(Date date){
        Date lastDayOfMonth = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        lastDayOfMonth = cal.getTime();
        return lastDayOfMonth;
    }

14.获取某个时间所在月的第一天

/**
     * 获取某个时间所在月的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date){
        Date firstDayOfMonth = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //设置日历中月份的第一天
        cal.set(Calendar.DAY_OF_MONTH, 1);
        firstDayOfMonth = cal.getTime();
        return firstDayOfMonth;
    }

15.获取某年的最后一天

 /**
     * 获取某年的最后一天
     * @param date
     * @return
     */
    public static Date getLastDayOfYear(Date date){
        Date lastDayOfMonth = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //获取某年最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_YEAR);
        //设置日历中年份的最大天数
        cal.set(Calendar.DAY_OF_YEAR, lastDay);
        lastDayOfMonth = cal.getTime();
        return lastDayOfMonth;
    }

16. 获取某年的第一天

 /**
     * 获取某年的第一天
     * @param date
     * @return
     */
    public static Date getFirstDayOfYear(Date date){
        Date lastDayOfMonth = null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        //设置日历中年份的最大天数
        cal.set(Calendar.DAY_OF_YEAR, 1);
        lastDayOfMonth = cal.getTime();
        return lastDayOfMonth;
    }

17.两个时间相差距离多少天多少小时

/**
     * 两个时间相差距离多少天多少小时
     * @param one
     * @param two
     * @return
     * @throws ParseException
     */
    public static String getDistanceTime2(Date one, Date two) throws ParseException {
        long day = 0;
        long hour = 0;
        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);
        return day + "天" + hour + "小时" ;
    }

你可能感兴趣的:(编程杂谈,java,数据库,面试,经验分享,恰饭)