table of Contents
Two, introduce maven dependency
I. Introduction
A scheduled task is a task that executes the planned work at the agreed time. This is the superficial meaning. In Linux, we often use cron server to complete this work. The cron server can perform specific tasks according to the time agreed in the configuration file.
During development, it is necessary to check the validity of cron expressions and obtain execution time. These functions are implemented in CronUtil.
Two, introduce maven dependency
<dependency>
<groupId>com.cronutils</groupId>
<artifactId>cron-utils</artifactId>
<version>9.1.3</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
Three, source code
package com.asyf.demo.other_api.cron.cronutils;
import com.cronutils.descriptor.CronDescriptor;
import com.cronutils.model.Cron;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.parser.CronParser;
import org.quartz.TriggerUtils;
import org.quartz.impl.triggers.CronTriggerImpl;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import static com.cronutils.model.CronType.QUARTZ;
public class CronUtil {
public static void main(String[] args) throws ParseException {
String expressiion = "0 0 0 ? 5 2#1";
//expressiion = "0 0 0 0 5 2#1";
//格式校验
boolean b = checkValid(expressiion);
System.out.println(b);
if (b) {
//解释cron表达式
String s = describeCron(expressiion);
System.out.println(s);
//获取下次运行时间
List<Date> nextExecTime = getNextExecTime(expressiion, 5);
nextExecTime.stream().forEach(d -> {
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(d));
});
}
}
/**
* 解释cron表达式
*/
public static String describeCron(String expressiion) {
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
CronParser parser = new CronParser(cronDefinition);
Cron cron = parser.parse(expressiion);
//设置语言
CronDescriptor descriptor = CronDescriptor.instance(Locale.CHINESE);
return descriptor.describe(cron);
}
/**
* 检查cron表达式的合法性
*
* @param cron cron exp
* @return true if valid
*/
public static boolean checkValid(String cron) {
try {
CronDefinition cronDefinition = CronDefinitionBuilder.instanceDefinitionFor(QUARTZ);
CronParser parser = new CronParser(cronDefinition);
parser.parse(cron);
} catch (IllegalArgumentException e) {
System.out.println(String.format("cron=%s not valid", cron));
return false;
}
return true;
}
/**
* @param cronExpression cron表达式
* @param numTimes 下一(几)次运行的时间
* @return
*/
public static List<Date> getNextExecTime(String cronExpression, Integer numTimes) throws ParseException {
List<String> list = new ArrayList<>();
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression(cronExpression);
// 这个是重点,一行代码搞定
return TriggerUtils.computeFireTimes(cronTriggerImpl, null, numTimes);
}
}
Four, test
true
在 00:00 在 五月 月 星期一 1 每个月
2021-05-03 00:00:00
2022-05-02 00:00:00
2023-05-01 00:00:00
2024-05-06 00:00:00
2025-05-05 00:00:00