失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > crontab工具类 断给定的时间 是否 满足 crontab 表达式.md

crontab工具类 断给定的时间 是否 满足 crontab 表达式.md

时间:2019-10-06 00:17:48

相关推荐

crontab工具类 断给定的时间 是否 满足 crontab 表达式.md

为什么80%的码农都做不了架构师?>>>

itone.crontab工具类.md

/*** 断给定的时间 是否 满足 crontab 表达式【忽略毫秒】* 可以接受指定秒数 误差**/public class CrontabUtils {private static final Logger LOGGER = LoggerFactory.getLogger(CrontabUtils.class);/*** 判断给定的时间 是否 满足 crontab 表达式【忽略毫秒】** @param date the date to evaluate* @return a boolean indicating whether the given date satisfies the cron* expression*/public static boolean isMatch(String crontabExpress, Date date) {try {CronExpression crontabExpression = new CronExpression(crontabExpress);return crontabExpression.isSatisfiedBy(date);} catch (ParseException e) {LOGGER.error(e.getMessage(), e);return false;}}public static boolean isMatch(CronExpression cronExpression, Date date) {return cronExpression.isSatisfiedBy(date);}/*** 判断给定的时间 是否 满足 crontab 表达式【忽略毫秒】 可以接受指定秒数 误差* @param cronExpression* @param date* @param secondsRange 可以接受指定秒数* @return*/public static boolean isMatchRange(CronExpression cronExpression,Date date,int secondsRange) {Calendar testDateCal = Calendar.getInstance(cronExpression.getTimeZone());testDateCal.setTime(date);testDateCal.set(Calendar.MILLISECOND, 0);Date originalDate = testDateCal.getTime();testDateCal.add(Calendar.SECOND, -secondsRange-1);Date timeAfter = cronExpression.getTimeAfter(testDateCal.getTime());return ((timeAfter != null) && (Math.abs(timeAfter.getTime()-originalDate.getTime())<=secondsRange*1000));}/*** 判断给定的时间 是否 满足 crontab 表达式【忽略毫秒】 可以接受指定秒数 误差*/public static boolean isMatchRange(CronExpression cronExpression,long seconds,int secondsRange) {return isMatchRange(cronExpression, new Date(seconds * 1000), secondsRange);}/*** 判断给定的时间 是否 满足 crontab 表达式【忽略毫秒】 可以接受指定秒数 误差*/public static boolean isMatchRange(String crontab,Date date,int secondsRange) {try {CronExpression crontabExpression = new CronExpression(crontab);return isMatchRange(crontabExpression,date,secondsRange);} catch (ParseException e) {LOGGER.error(e.getMessage(), e);return false;}}}

如果觉得《crontab工具类 断给定的时间 是否 满足 crontab 表达式.md》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。