1.Spring 自带的
/**
* 计算cron表达式近n次时间-(6段-秒 分 时 日 月 周)
*
* @param cronExpression cron表达式
* @param cronCount 下几次运行时间
* @param maxCronCount 最大执行次数
* @return List<String>
*/
@RequestMapping(value = "listCronRunTime")
public AjaxResult listCronRunTime(
@RequestParam(value = "cronExpression", required = false) String cronExpression,
@RequestParam(value = "cronCount", required = false, defaultValue = "1") Integer cronCount,
@RequestParam(value = "maxCronCount", required = false, defaultValue = "100") Integer maxCronCount) {
if (StringUtils.isBlank(cronExpression)) {
return new AjaxResult(-1, "Cron 表达式不能为空!");
}
if (!CronSequenceGenerator.isValidExpression(cronExpression)) {
return new AjaxResult(-1, "Cron格式不正确,Cron:" + cronExpression);
}
if (cronCount < 1) {
cronCount = 1;
}
if (maxCronCount < 1) {
maxCronCount = 100;
}
if (cronCount > maxCronCount) {
cronCount = maxCronCount;
}
CronSequenceGenerator cronSequenceGenerator = new CronSequenceGenerator(cronExpression);
List<String> cronTimeList = new ArrayList<>();
Date nextTimePoint = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (int i = 0; i < cronCount; i++) {
nextTimePoint = cronSequenceGenerator.next(nextTimePoint);
cronTimeList.add(sdf.format(nextTimePoint));
}
return new AjaxResult(cronTimeList);
}
2.第二种 Quartz
首先引入maven依赖或者引入jar(本来就有定时任务的,此步骤省略)
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
(1).
/**
*
* @param cronExpression cron表达式
* @param numTimes 下几次运行时间
* @return
*/
public static List<String> getNextExecTime(String cronExpression,Integer numTimes) {
List<String> list = new ArrayList<>();
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
try {
cronTriggerImpl.setCronExpression(cronExpression);
} catch(ParseException e) {
e.printStackTrace();
}
// 这个是重点,一行代码搞定
List<Date> dates = TriggerUtils.computeFireTimes(cronTriggerImpl, null, numTimes);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (Date date : dates) {
list.add(dateFormat.format(date));
}
return list;
}
(2).
/**
* 最近n次运行时间 Date[]-(7段-秒 分 时 日 月 周 年)
*
* @param cron 表达式
* @param count 运行次数
*/
public static Date[] getCronScheduledDate(String cron, Integer count) {
if (!CronExpression.isValidExpression(cron)) {
System.out.println("定时任务Cron格式不正确,Cron:" + cron);
return null;
}
if (count == null || count < 1) {
count = 1;
}
Date[] dateArray = new Date[count];
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("Caclulate Date").withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
Date time0 = trigger.getStartTime();
Date time1 = trigger.getFireTimeAfter(time0);
dateArray[0] = time1;
if (dateArray.length > 1) {
Date timeTemp = time1;
for (int i = 1; i < dateArray.length; i++) {
timeTemp = trigger.getFireTimeAfter(timeTemp);
if (timeTemp != null) {
dateArray[i] = timeTemp;
} else {
break;
}
}
}
return dateArray;
}
检验表达式是否正确(spring + quartz)
/**
* 检验表达式是否正确-spring(org.springframework.scheduling.support.CronSequenceGenerator)
*
* @param cron cron表达式
* @return 正确:true
*/
public static boolean cronValidExpressionFlag(String cron) {
return StringUtils.isNotBlank(cron) && CronSequenceGenerator.isValidExpression(cron);
}
/**
* 检验表达式是否正确-quartz(org.quartz.CronExpression)
*
* @param cron cron表达式
* @return 正确:true
*/
public static boolean cronValidExpressionFlag(String cron) {
return StringUtils.isNotBlank(cron) && CronExpression.isValidExpression(cron);
}