下面列出了怎么用org.quartz.TriggerUtils的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* 根据Cron表达式获取接下来的最近几次触发时间
* @param numTimes The number of next fire times to produce
* Comment by 玄玉<https://jadyer.cn/> on 2018/11/15 11:02.
*/
List<Date> getNextFireTimes(String cron, int numTimes) {
if(!CronExpression.isValidExpression(cron)){
throw new IllegalArgumentException("CronExpression不正确");
}
List<String> list = new ArrayList<>();
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
try {
cronTriggerImpl.setCronExpression(cron);
} catch (ParseException e) {
throw new SeedException(CodeEnum.SYSTEM_BUSY.getCode(), "使用表达式["+cron+"]初始化CronTrigger时出错", e);
}
return TriggerUtils.computeFireTimes(cronTriggerImpl, null, numTimes);
}
public static List<String> getRecentTriggerTime(String cron) {
List<String> list = new ArrayList<String>();
try {
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression(cron);
List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, 5);
for (Date date : dates) {
list.add(DateUtils.parseStringFromDate(date,DateUtils.format));
}
} catch (ParseException e) {
e.printStackTrace();
}
return list;
}