这个需求的内容是:先存储redis,若果redis存储失败,发送短信告警,并且发短信的时间设定为时间戳,并且发短信的时间间隔为十分钟,十分钟之内若存储redis失败,统计次数,并且不发送短信,若大于十分钟,发送短信。

            //直接存储Redis
            try {
                 jedis = jedisConfigBean.getJedis();
                String key = DigestUtils.md5DigestAsHex((REDISKEY + rzMarkTimeData.getSvcNumber()).getBytes());
                jedis.set(key, GsonUtils.toJson(rzMarkTimeData));
                jedis.expire(key, FAILURETIME);
            } catch (Exception e) {
                try {
                    //统计redis存入异常次数
                    errorTimes = ++errorTimes;
                    if (StringUtils.isBlank(errorTime)) {//如果errorTime的为0,说明errorTime没有存储
                        //设置当前时间为时间戳,并把时间戳存储到errorTime中,并且发送短信
                        errorTime = TimestampUtils.format(new Date(), TimestampUtils.DATE_NORMAL_TIME);
                        SMSAlarmUtil.sendMessage("用户登录行为入redis异常,异常次数为:"+errorTimes, SMSAlarmUtil.ORDERTYDIC);
                        errorTimes = 0;
                    } else if (StringUtils.isNotEmpty(errorTime)){//如果errorTime不是空白,说明errorTime已经存储了一个时间戳
                        //进行比较
                        //获取到旧的时间戳,并且加上10分钟
                        String oldTime = DateUtil.addDateMinuteStr(errorTime, 10);
                        String newTime = TimestampUtils.format(new Date(), TimestampUtils.DATE_NORMAL_TIME);
                        if (newTime.compareTo(oldTime) < 0){//新的时间在10分钟以内
                            return;
                        }
                        if (newTime.compareTo(oldTime) >= 0){//新的时间大于等于10分钟
                            SMSAlarmUtil.sendMessage("用户登录行为入redis异常,异常次数为:"+errorTimes, SMSAlarmUtil.ORDERTYDIC);
                            //更新错误时间戳
                            errorTime = newTime;
                            errorTimes = 0;
                        }
                    }
                } catch (Exception exception) {
                    logger.error("告警短信大于等于10分钟发送失败",exception);
                }
                logger.error("直接存储redis失败",e);
            }finally {
                 jedis.close();
            }

 时间工具类:

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

public class TimestampUtils {

    public static final String DATE_TIME = "yyyyMMddHHmmss";
    public static final String DATE_TIME_FAR = "yyyyMMddHHmmssSSS";
    public static final String DATE_NORMAL_TIME = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FULL_TIME = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATE_TO_MINUTE = "yyyyMMddHHmm";
    public static final String DATE_TO_HOUR = "yyyyMMddHH";
    public static final String DATE = "yyyyMMdd";
    public static final String DATE2 = "yyyy-MM-dd";
    public static final String YEAR_MONTH = "yyyyMM";
    public static final String TIME = "HHmmss";
    public static final Timestamp EFF_DATE;
    public static final String EEEE="EEEE";
    public static final Timestamp EXP_DATE;

    static {
        Timestamp effDate = null;
        Timestamp expDate = null;
        try {
            effDate = new Timestamp(0);
            expDate = TimestampUtils.parse("20371231235959", DATE_TIME);
        } catch (ParseException e) {
            ShutdownHook.exit(1);
        }

        EFF_DATE = effDate;
        EXP_DATE = expDate;
    }

    /**
     * 字符串转日期
     *
     * @param date
     * @param pattern
     * @return
     * @throws ParseException
     */

    public static Timestamp parse(String date, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return new Timestamp(sdf.parse(date).getTime());
    }

    /**
     * 字符串转日期
     *
     * @param date
     * @param pattern
     * @return
     * @throws ParseException
     */

    public static Date parseDate(String date, String pattern) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.parse(date);
    }

    /**
     * 日期转字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String format(Date date, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    /**
     * 计算给定日期偏移指定年数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移年数,正值增加,负值减少
     * @return
     */
    public static Timestamp addYears(Date date, int amount) {
        return new Timestamp(DateUtils.addYears(date, amount).getTime());
    }

    /**
     * 计算给定日期偏移指定月数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移月数,正值增加,负值减少
     * @return
     */
    public static Timestamp addMonths(Date date, int amount) {
        return new Timestamp(DateUtils.addMonths(date, amount).getTime());
    }

    /**
     * 计算给定日期偏移指定星期数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移星期数,正值增加,负值减少
     * @return
     */
    public static Timestamp addWeeks(Date date, int amount) {
        return new Timestamp(DateUtils.addWeeks(date, amount).getTime());
    }

    /**
     * 计算给定日期偏移指定日数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移日数,正值增加,负值减少
     * @return
     */
    public static Timestamp addDays(Date date, int amount) {
        return new Timestamp(DateUtils.addDays(date, amount).getTime());
    }

    /**
     * 计算给定日期偏移指定小时数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移小时数,正值增加,负值减少
     * @return
     */

    public static Timestamp addHours(Date date, int amount) {
        return new Timestamp(DateUtils.addHours(date, amount).getTime());
    }

    /**
     * 计算给定日期偏移指定分钟数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移分钟数,正值增加,负值减少
     * @return
     */

    public static Timestamp addMinutes(Date date, int amount) {
        return new Timestamp(DateUtils.addMinutes(date, amount).getTime());
    }

    /**
     * 计算给定日期偏移指定秒数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移秒数,正值增加,负值减少
     * @return
     */

    public static Timestamp addSeconds(Date date, int amount) {
        return new Timestamp(DateUtils.addSeconds(date, amount).getTime());
    }

    /**
     * 计算给定日期偏移指定毫秒数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移毫秒数,正值增加,负值减少
     * @return
     */

    public static Timestamp addMilliseconds(Date date, int amount) {
        return new Timestamp(DateUtils.addMilliseconds(date, amount).getTime());
    }

    /**
     * 日期截断
     *
     * @param date  给定日期
     * @param field
     * @return
     * @see Calendar
     * @see DateUtils
     */
    public static Timestamp truncate(Date date, int field) {
        return new Timestamp(DateUtils.truncate(date, field).getTime());
    }

    /**
     * 日期向上取整
     *
     * @param date  给定日期
     * @param field
     * @return
     * @see Calendar
     * @see DateUtils
     */
    public static Timestamp ceiling(Date date, int field) {
        return new Timestamp(DateUtils.ceiling(date, field).getTime());
    }

    /**
     * 日期四舍五入
     *
     * @param date  给定日期
     * @param field
     * @return
     * @see Calendar
     * @see DateUtils
     */
    public static Timestamp round(Date date, int field) {
        return new Timestamp(DateUtils.round(date, field).getTime());
    }

    /**
     * 计算星期
     *
     * @param timestamp 给定日期
     * @return 周一 = 1 周日 = 7
     */
    public static int getDayOfWeek(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        int num = calendar.get(Calendar.DAY_OF_WEEK);
        if (1 == num)
            num = 7;
        else
            num = num - 1;
        return num;
    }

    /**
     * 获取日期的年,格式: YYYY, 例如: 2016
     *
     * @param timestamp 给定日期
     * @return
     */
    public static int getYear(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取日期的年,格式: YYYYMM, 例如: 201601
     *
     * @param timestamp 给定日期
     * @return
     */
    public static int getYearMonth(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        return calendar.get(Calendar.YEAR) * 100 + calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取日期的月份,格式: 1 - 12
     *
     * @param timestamp 给定日期
     * @return
     */
    public static int getMonth(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        return calendar.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取日期的天,格式: 1 - 31
     *
     * @param timestamp 给定日期
     * @return
     */
    public static int getDay(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取日期的小时,格式: 0 - 23
     *
     * @param timestamp 给定日期
     * @return
     */
    public static int getHour(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    /**
     * 获取日期的分钟,格式: 0 - 59
     *
     * @param timestamp 给定日期
     * @return
     */
    public static int getMinute(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        return calendar.get(Calendar.MINUTE);
    }

    /**
     * 获取日期的秒,格式: 0 - 60
     *
     * @param timestamp 给定日期
     * @return
     */
    public static int getSecond(Timestamp timestamp) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(timestamp);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 是否同一天
     *
     * @param left
     * @param right
     * @return
     */
    public static boolean isSameDay(Timestamp left, Timestamp right) {
        return TimestampUtils.isSameDay(left, right);
    }

    /**
     * 是否同一月
     *
     * @param left
     * @param right
     * @return
     */
    public static boolean isSameMonth(Timestamp left, Timestamp right) {
        final Calendar cal1 = Calendar.getInstance();
        cal1.setTime(left);
        final Calendar cal2 = Calendar.getInstance();
        cal2.setTime(right);

        return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) &&
                cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) &&
                cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH));
    }

    /**
     * 获取给定日期之间月数,不包含当前月(非整月算一个月,精确到月)
     *
     * @param beginTime 起始时间
     * @param endTime   结束时间
     * @return 月数
     */
    public static int monthsBetween(Timestamp beginTime, Timestamp endTime) {
        int years = TimestampUtils.getYear(endTime) - TimestampUtils.getYear(beginTime);
        int months = TimestampUtils.getMonth(endTime) - TimestampUtils.getMonth(beginTime);
        return years * 12 + months;
    }

    public static int daysBetween(Timestamp beginTime, Timestamp endTime) {
        int rawOffset = TimeZone.getDefault().getRawOffset();
        return (int) ((endTime.getTime() + rawOffset) / 86400000 - (beginTime.getTime() + rawOffset) / 86400000);
    }

    public static int hoursBetween(Timestamp beginTime, Timestamp endTime) {
        return (int) (endTime.getTime() / 3600000 - beginTime.getTime() / 3600000);
    }

    public static int minutesBetween(Timestamp beginTime, Timestamp endTime) {
        return (int) (endTime.getTime() / 60000 - beginTime.getTime() / 60000);
    }

    public static int secondsBetween(Timestamp beginTime, Timestamp endTime) {
        return (int) (endTime.getTime() / 1000 - beginTime.getTime() / 1000);
    }

    /**
     * 判断日期是否有效(effDate <= timestamp < expDate
     *
     * @param timestamp 时间戳
     * @param effDate   生效时间
     * @param expDate   失效时间
     * @return
     */
    public static boolean valid(Timestamp timestamp, Timestamp effDate, Timestamp expDate) {
        return timestamp.compareTo(effDate) >= 0 && (expDate == null || timestamp.compareTo(expDate) < 0);
    }

    /**
     * 判断日期是否有效(effDate < timestamp < expDate
     *
     * @param timestamp 时间戳
     * @param effDate   生效时间
     * @param expDate   失效时间
     * @return
     */
    public static boolean validO(Timestamp timestamp, Timestamp effDate, Timestamp expDate) {
        return timestamp.compareTo(effDate) > 0 && (expDate == null || timestamp.compareTo(expDate) < 0);
    }

    /**
     * 判断日期是否在前
     *
     * @param left
     * @param right
     * @return
     */
    public static boolean before(Timestamp left, Timestamp right) {
        if (left == null)
            return false;
        else if (right == null)
            return true;
        else
            return left.before(right);
    }

    /**
     * 判断日期是否在后
     *
     * @param left
     * @param right
     * @return
     */
    public static boolean after(Timestamp left, Timestamp right) {
        if (left == null)
            return true;
        else if (right == null)
            return false;
        else
            return left.after(right);
    }

    /**
     * 判断账期是否有效:
     * effDate(yyyymm) <= acctCycle <= expDate(yyyymm,减1秒)
     * effDate(yyyymmdd) <= acctCycle <= expDate(yyyymmdd,减1秒)
     *
     * @param acctCycle 账期 yyyymm(dd)
     * @param effDate   生效时间
     * @param expDate   失效时间
     * @return
     */
    public static boolean valid(String acctCycle, Timestamp effDate, Timestamp expDate) {
        String pattern = acctCycle.length() == 6 ? TimestampUtils.YEAR_MONTH : TimestampUtils.DATE;

        String effCycle = TimestampUtils.format(effDate, pattern);
        if (acctCycle.compareTo(effCycle) < 0)
            return false;

        if (expDate == null)
            return true;

        String expCycle = TimestampUtils.format(TimestampUtils.addSeconds(expDate, -1), pattern);
        return (acctCycle.compareTo(expCycle) <= 0);
    }

    /**
     * 获取下个月1号时间
     *
     * @param nowMonthFirst
     * @return
     */
    public static Timestamp getNextMonthFirst(Timestamp nowMonthFirst) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowMonthFirst);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取上个月1号时间
     *
     * @param nowMonthFirst
     * @return
     */
    public static Timestamp getLastMonthFirst(Timestamp nowMonthFirst) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowMonthFirst);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取下下个月1号时间
     *
     * @param nowMonthFirst
     * @return
     */
    public static Timestamp getSecondMonthFirst(Timestamp nowMonthFirst) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowMonthFirst);
        calendar.add(Calendar.MONTH, 2);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取下下个月1号时间
     *
     * @param nowMonthFirst
     * @return
     */
    public static Timestamp getMonthSecond(Timestamp nowMonthFirst) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowMonthFirst);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 2);
        return new Timestamp(calendar.getTimeInMillis());
    }

    public static Timestamp getExpDate(Timestamp now, int monthAdd) {
        return addMonths(truncate(now, Calendar.MONTH), monthAdd);
    }

    public static Timestamp now() {
        return new Timestamp(new Date().getTime());
    }

    /**
     * 计算给定日期偏移指定月数后的日期
     *
     * @param date   给定日期
     * @param amount 偏移月数,正值增加,负值减少
     * @return
     */
    public static Timestamp add(Date date, int amount, String unit) {
        Timestamp timestamp = null;
        try {
            switch (unit) {
                case "year":
                    timestamp = addYears(date, amount);
                    break;
                case "month":
                    timestamp = addMonths(date, amount);
                    break;
                case "day":
                    timestamp = addDays(date, amount);
                    break;
                case "week":
                    timestamp = addWeeks(date, amount);
                    break;
                case "hour":
                    timestamp = addHours(date, amount);
                    break;
                case "minute":
                    timestamp = addMinutes(date, amount);
                    break;
                case "second":
                    timestamp = addSeconds(date, amount);
                    break;
                case "millisecond":
                    timestamp = addMilliseconds(date, amount);
                    break;
                default:
                    timestamp = addMonths(date, 1);
                    break;
            }
        }catch (Exception e){
            Timestamp now = new Timestamp(Calendar.getInstance(TimeZone.getTimeZone("GMT+8")).getTimeInMillis());
            timestamp=addMonths(now,1);
        }
        return timestamp;
    }

    /**
     * 比较时间先后
     * 和当前时间作比较
     * 小于当前时间返回true 否则返回false
     * 为空返回true
     *
     * @param beginStr   开始时间
     * @return
     */
    public static boolean compareTime(String beginStr) {
        if (StringUtils.isEmpty(beginStr)) {
            return true;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_NORMAL_TIME);
        try {
            Date begin = simpleDateFormat.parse(beginStr);
            if (new Date().after(begin)) {
                return true;
            }
        } catch (Exception e) {
            return true;
        }
        return false;
    }
    /**
     * 比较时间先后
     * 和当前时间作比较
     * 当beginStr大于当前时间时,返回TRUE,当小于等于时,返回false
     * 为空返回true
     *
     * @param beginStr   开始时间
     * @return
     */
    public static boolean compareTimeBefore(String beginStr) {
        if (StringUtils.isEmpty(beginStr)) {
            return true;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DATE_NORMAL_TIME);
        try {
            Date begin = simpleDateFormat.parse(beginStr);
            if (begin.after(new Date())) {
                return true;
            }
        } catch (Exception e) {
            return true;
        }
        return false;
    }
    /**
     * 判断当前时间是否在指定范围时间内
     *
     * @param businessTime 时间段  12:00--13:00
     * @return true-在  false-不在
     */
    public static Boolean judgeTheTimeRange(String businessTime) {
        try {
            String[] times = businessTime.split("--");
            String[] minTime = times[0].split(":");
            String[] maxTime = times[1].split(":");
            //当前时间
            Calendar currentDate = Calendar.getInstance();
            currentDate.setTime(new Date());
            //开始时间
            Calendar min = Calendar.getInstance();
            min.set(Calendar.YEAR, currentDate.get(Calendar.YEAR));
            min.set(Calendar.MONTH, currentDate.get(Calendar.MONTH));
            min.set(Calendar.DAY_OF_MONTH, currentDate.get(Calendar.DAY_OF_MONTH));
            min.set(Calendar.HOUR_OF_DAY, Integer.parseInt(minTime[0]));
            min.set(Calendar.MINUTE, Integer.parseInt(minTime[1]));
            min.set(Calendar.SECOND, 0);
            min.set(Calendar.MILLISECOND, 0);
            //结束时间
            Calendar max = Calendar.getInstance();
            max.set(Calendar.YEAR, currentDate.get(Calendar.YEAR));
            max.set(Calendar.MONTH, currentDate.get(Calendar.MONTH));
            max.set(Calendar.DAY_OF_MONTH, currentDate.get(Calendar.DAY_OF_MONTH));
            max.set(Calendar.HOUR_OF_DAY, Integer.parseInt(maxTime[0]));
            max.set(Calendar.MINUTE, Integer.parseInt(maxTime[1]));
            max.set(Calendar.SECOND, 0);
            max.set(Calendar.MILLISECOND, 0);

            if (max.getTimeInMillis() >= min.getTimeInMillis()) {
                if (currentDate.getTimeInMillis() >= min.getTimeInMillis() && currentDate.getTimeInMillis() <= max.getTimeInMillis()) {
                    return true;
                } else {
                    return false;
                }
            } else {
                max.set(Calendar.DAY_OF_MONTH, currentDate.get(Calendar.DAY_OF_MONTH) + 1);
                if (currentDate.getTimeInMillis() >= min.getTimeInMillis() && currentDate.getTimeInMillis() <= max.getTimeInMillis()) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (Exception e) {
            return false;
        }

    }

    /**
     * 判断是否是活动时间周
     *
     * @param week
     * @return
     */
    public static boolean judgeTheTimeRangeWeek(String week) {
        //String a="星期一Monday|12:00--24:00,星期二Tuesday|00:00--24:00,星期三Wednesday|00:00--24:00,星期四Thursday|00:00--24:00,星期五Friday|00:00--24:00,星期六Saturday|00:00--24:00,星期日Sunday|00:00--24:00";
        String[] split = StringUtils.split(week,",", -1);
        for (String s : split) {
            String[] split1 = StringUtils.split(s, "|", -1);
            if (split1[0].contains(TimestampUtils.format(new Date(), TimestampUtils.EEEE))) {
                if (TimestampUtils.judgeTheTimeRange(split1[1])) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是否是活动时间天
     *
     * @param day
     * @return
     */
    public static boolean judgeTheTimeRangeDay(String day) {
        //String a="20211229$12:00--24:00,20211230$00:00--24:00";
        String[] split = StringUtils.split(day, ",", -1);
        for (String s : split) {
            String[] split1 = StringUtils.split(s, "$", -1);
            if (TimestampUtils.format(new Date(), TimestampUtils.DATE).equals(split1[0])) {
                if (TimestampUtils.judgeTheTimeRange(split1[1])) {
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 判断是否是活动时间
     *
     * @param day
     * @return
     */
    public static boolean judgeTheTimeRangeToDay(String day) {
        //String a="12:00--24:00,200:00--24:00";
        String[] split = StringUtils.split(day, ",", -1);
        for (String s : split) {
            if (TimestampUtils.judgeTheTimeRange(s)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 时间格式转化(String String)
     *
     * @param loginTime 时间 yyyyMMddHHmmss
     * @return 转化后的时间  yyyy-MM-dd HH:mm:ss
     */
    public static String timeConversion(String loginTime) {
        SimpleDateFormat formatterNew = new SimpleDateFormat(DATE_NORMAL_TIME);
        SimpleDateFormat formatterOld = new SimpleDateFormat(DATE_TIME);
        try{
            return formatterNew.format(formatterOld.parse(loginTime));
        }catch (Exception e){
            return formatterNew.format(new Date());
        }
    }

    /**
     * 判断两个时间是否在有效期内
     *
     * @param detectionTime 开始时间 2022-01-19 00:00:00
     * @param invalidTime 结束时间 2022-01-19 23:59:59
     * @return
     */
    public static boolean twotimeRange(String detectionTime,String invalidTime) {
        SimpleDateFormat formatterNew = new SimpleDateFormat(DATE_NORMAL_TIME);
        try{
            Date date = new Date();
            if(formatterNew.parse(detectionTime).getTime()<date.getTime() && date.getTime()<formatterNew.parse(invalidTime).getTime()){
                return true;
            }
            return false;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 时间过期判断(过期为true)
     * @param time
     */
    public static boolean timeInvalidTime(String time)  {
        SimpleDateFormat formatterNew = new SimpleDateFormat(DATE_FULL_TIME);
        boolean flag = false;
        try{
            Calendar calendar =Calendar.getInstance();
            calendar.setTime(new Date());
            calendar.add(Calendar.DATE, -30);
            Date minDate = calendar.getTime();

            if(minDate.getTime()>formatterNew.parse(time).getTime()){
                flag = true;
            }
        }catch (Exception e){
            return flag;
        }
        return flag;
    }

    /**
     * 判断time时间是否是localTime目标时间前30分钟内
     * @param time   20220113145027
     * @param localTime  yyyy-MM-dd HH:mm:ss.SSS
     * @return
     */
    public static boolean timeInvalid(String time,String localTime)  {
        SimpleDateFormat formatterNew = new SimpleDateFormat(DATE_FULL_TIME);
        SimpleDateFormat formatter = new SimpleDateFormat(DATE_TIME);
        boolean flag = false;
        try{
            Date date = formatterNew.parse(localTime);
            Calendar calendar =Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.MINUTE, -30);
            Date minDate = calendar.getTime();
            if(minDate.getTime() < formatter.parse(time).getTime() && formatter.parse(time).getTime() < date.getTime()){
                flag = true;
            }
        }catch (Exception e){
            return flag;
        }
        return flag;
    }


    public static void main(String[] args)  {
        System.out.println(judgeTheTimeRangeWeek("星期五Thursday|10:00--17:00,星期五Thursday|17:00--18:20,星期五Thursday|17:00--17:10"));
    }

}

 

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
public class DateUtils {

    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String dateToStr(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String dateString = formatter.format(date);
            return dateString;
        }
    }
    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss SSS
     */
    public static String dateToStr2(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String dateToStr1(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String dateToString(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(date);
            return dateString;
        }
    }
    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String dateToStrY_M_D(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyyMMdd
     */
    public static String dateToString2(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd HH
     */
    public static String dateToString3(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyyMMddHH
     */
    public static String dateToString4(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHH");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyyMMddHH
     */
    public static String dateToStr5(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * String转date
     *
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDate(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDate, pos);
            return strtodate;
        }
    }

    /**
     * String转date
     *
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String strToString(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            ParsePosition pos = new ParsePosition(0);
            Date date = format.parse(strDate, pos);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

            return formatter.format(date);
        }
    }

    /**
     * String转date
     *
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDate1(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDate, pos);
            return strtodate;
        }
    }

    /**
     * String转date
     *
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDate2(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDate, pos);
            return strtodate;
        }
    }

    /**
     * 给时间加上几个自然月
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addDateMonth(Date date, int month) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        date = cal.getTime();
        cal = null;
        return date;
    }

    /**
     * 给时间加上几天
     *
     * @param day 时间 格式:yyyy-MM-dd HH:mm:ss
     * @param day 需要加的时间
     * @return
     */
    public static String addDateDay(Date date, int day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);// 24小时制
        date = cal.getTime();
        cal = null;
        return format.format(date);
    }

    /**
     * 给时间加上几天
     *
     * @param day 时间 格式:yyyy-MM-dd HH:mm:ss
     * @param day 需要加的时间
     * @return
     */
    public static Date addDay(Date date, int day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);// 24小时制
        date = cal.getTime();
        cal = null;
        return date;
    }

    /**
     * 给时间加上几天
     *
     * @param day 时间 格式:yyyy-MM-dd HH:mm:ss
     * @param day 需要加的时间
     * @return
     */
    public static Date addDayDate(String dateStr, int day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date date = format.parse(dateStr, pos);
        if (date == null)
            return null;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, day);// 24小时制
        date = cal.getTime();
        cal = null;
        return date;
    }

    /**
     * 给时间增加几个小时
     *
     * @param hour
     * @return
     */
    public static Date addDateMinut(Date date, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (date == null) return date;
        System.out.println("front:" + format.format(date)); //显示输入的日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        System.out.println("after:" + format.format(date));  //显示更新后的日期
        cal = null;
        return date;

    }
    /**
     * 给时间加上几个小时
     *
     * @param day  时间 格式:yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public static String addDateMinutStr(String day, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        cal = null;
        return format.format(date);
    }
    /**
     * 给时间加分钟
     * @param day  时间 格式:yyyy-MM-dd HH:mm:ss
     * @param minute 需要加的时间
     * @return
     */
    public static String addDateMinuteStr(String day, int minute) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = format.parse(day);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (date == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MINUTE, minute);// 24小时制
        date = cal.getTime();
        cal = null;
        return format.format(date);
    }

    /**
     * String转date
     *
     * @param strDate yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static Date strToDateyyyyMMddHHmmss(String strDate) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
            ParsePosition pos = new ParsePosition(0);
            Date strtodate = formatter.parse(strDate, pos);
            return strtodate;
        }
    }

    /**
     * 获取时间的日
     *
     * @param date
     * @return
     */
    public static Integer getDay(Date date) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        return now.get(Calendar.DAY_OF_MONTH);
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd HH:mm:ss
     */
    public static String dateToStr6(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd\u2060\u2062\u2062 \u2063\u2061HH:mm:ss");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /**
     * date转String
     *
     * @param date
     * @return yyyy-MM-dd
     */
    public static String dateToStrSSS(Date date) {
        if (date == null) {
            return "";
        } else {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmssSSS");
            String dateString = formatter.format(date);
            return dateString;
        }
    }

    /****
     * 传入具体日期 ,返回具体日期增加一个月。
     * @param date 日期(2017-04-13)
     * @return 2017-05-13
     * @throws ParseException
     */
    public static String subMonth(String date, Integer days) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = null;
        try {
            dt = sdf.parse(date);
        } catch (ParseException e) {
            log.error("月份增加错误", e);
        }
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, days);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        return reStr;
    }

    /**
     * 获取两个日期之间的所有日期
     *
     * @param startTime
     *            开始日期
     * @param endTime
     *            结束日期
     * @return
     */
    public static List<String> getDays(String startTime, String endTime) {

        // 返回的日期集合
        List<String> days = new ArrayList<String>();

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

        return days;
    }

    /**
     * 日期添加到分钟得到新时间
     * @param date 开始时间
     * @param x	  相隔分钟数
     * @return
     *
     */
    public static Date addDateMinute(Date date, int x) {
        if (date == null) return date;
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 24小时制
        cal.add(Calendar.MINUTE, x);
        //得到结算后的结果 yyyy-MM-dd HH:mm
        date = cal.getTime();
        cal = null;
        return date;
    }
    /**
     * 给时间加上几个小时
     *
     * @param day  时间 格式:yyyy-MM-dd HH:mm:ss
     * @param hour 需要加的时间
     * @return
     */
    public static String addDateHour(Date day, int hour) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = day;
        if (date == null)
            return "";
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR, hour);// 24小时制
        date = cal.getTime();
        cal = null;
        return format.format(date);
    }
    public static void main(String[] args) {
        List<String> days = getDays("2021-11-11", "2021-12-12");
        System.out.println(days.size());
        for (String day : days) {
            System.out.println("day = " + day);
//            System.out.println("day = " + day.substring(8,10));
        }
//        Date date = addDateMinute(new Date(), 10);
    }

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐