Quartz cron定时表达式解析成最近10次运行时间字符串

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 ;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章