Quartz cron定時表達式解析成最近10次運行時間字符串
Quartz cron定時表達式解析成最近10次運行時間字符串
public static List<String> cronExpression2executeDates(String cronExpression) throws ParseException {
List<String> resultList = new ArrayList<String>() ;
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression(cronExpression);//這裏寫要準備猜測的cron表達式
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
calendar.add(Calendar.YEAR, 2);//把統計的區間段設置爲從現在到2年後的今天(主要是爲了方法通用考慮,如那些1個月跑一次的任務,如果時間段設置的較短就不足20條)
List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, now, calendar.getTime());//這個是重點,一行代碼搞定~~
System.out.println(dates.size());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i =0;i < dates.size();i ++){
if(i >19){//這個是提示的日期個數
break;
}
resultList.add(dateFormat.format(dates.get(i)));
}
return resultList ;
}