1.判断cron 表达式是否在当前时间执行
例如: cron(0 0 0 * * ? *) 是否在今天执行
public static boolean getLastRunTimeByCron(String cron) {
List<Date> dates = null;
try {
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression(cron);
dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, 1);
} catch (ParseException e) {
e.printStackTrace();
}
if (dates != null && dates.size() != 0) {
Date date = dates.get(0);
//传入时间
//LocalDate dDate = localDate;
long epochMilli = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
Date firstDate = new Date(epochMilli);
Date lastDate = new Date(epochMilli+86400000);
if (date.before(lastDate) && date.after(firstDate)) {
return true;
}else if (date.compareTo(firstDate) == 0 || date.compareTo(lastDate) == 0) {
return true;
}else {
return false;
}
}else {
return false;
}
}
2、注意
参数可传入时间,判断cron是否在特定时间执行
//传入时间
LocalDate dDate = localDate;
long epochMilli = LocalDateTime.of(*dDate*, LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
3、cron表达式翻译
//通过Cron翻译
public String getByCron(String cron) {
String cronExplain = null;
if (StringUtils.isNotEmpty(CronExpParser.translateToChinese(cron))){
cronExplain = CronExpParser.translateToChinese(cron);
}
return cronExplain;
}
引入工具类 CronExpParser