springboot集成了Quartz定时任务,我们只要加个@Scheduled注解,就可以很方便的使用了,现实生活中,可能会遇到这样一种情况:想看查看下定时任务执行的最近20、30、50条记录等,本文参考了一下连接的博客
https://www.cnblogs.com/wulm/p/6923545.html
首先,
@Value("${schedule.corn.failReq}") String cron;
获取配置文件的表达式,
其次,关键代码,获取最近的执行时间
private static List<String> seeExcuteTime(String cron) { if (StringUtils.isEmpty(cron)) throw new IllegalArgumentException("参数不能为空"); CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cron); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); List<String> list = new ArrayList<>(20); //设置多少条记录 Date nextTimePoint = new Date(); for (int i=0; i<20; i++) { //计算下次时间点的开始时间 nextTimePoint = cronSequenceGenerator.next(nextTimePoint); list.add(sdf.format(nextTimePoint)); } return list; }
最后,打印出来
List<String> strings = seeExcuteTime(cron); for (String s : strings) { System.out.println(s); }