1、cron表达式详解
Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义, Cron有如下两种语法格式:
(1) Seconds Minutes Hours DayofMonth Month DayofWeek Year
(2)Seconds Minutes Hours DayofMonth Month DayofWeek
2、cron表达式结构
2.1 每隔字符含义
corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份
2.2 各字段的含义
字段 | 允许值 | 允许的特殊字符 |
秒(Seconds) | 0~59的整数 | , - * / 四个字符 |
分(Minutes) | 0~59的整数 | , - * / 四个字符 |
小时(Hours) | 0~23的整数 | , - * / 四个字符 |
日期(DayofMonth) | 1~31的整数(但是你需要考虑你月的天数) | ,- * ? / L W C 八个字符 |
月份(Month) | 1~12的整数或者 JAN-DEC | , - * / 四个字符 |
星期(DayofWeek) | 1~7的整数或者 SUN-SAT (1=SUN) | , - * ? / L C # 八个字符 |
年(可选,留空)(Year) | 1970~2099 | , - * / 四个字符 |
3、注意事项
每一个域都使用数字,但还可以出现如下特殊字符,它们的含义是:
(1)*:表示匹配该域的任意值。假如在Minutes域使用*, 即表示每分钟都会触发事件。
(2)?:只能用在DayofMonth和DayofWeek两个域。它也匹配域的任意值,但实际不会。因为DayofMonth和DayofWeek会相互影响。例如想在每月的20日触发调度,不管20日到底是星期几,则只能使用如下写法: 13 13 15 20 * ?, 其中最后一位只能用?,而不能使用*,如果使用*表示不管星期几都会触发,实际上并不是这样。
(3)-:表示范围。例如在Minutes域使用5-20,表示从5分到20分钟每分钟触发一次
(4)/:表示起始时间开始触发,然后每隔固定时间触发一次。例如在Minutes域使用5/20,则意味着5分钟触发一次,而25,45等分别触发一次.
(5),:表示列出枚举值。例如:在Minutes域使用5,20,则意味着在5和20分每分钟触发一次。
(6)L:表示最后,只能出现在DayofWeek和DayofMonth域。如果在DayofWeek域使用5L,意味着在最后的一个星期四触发。
(7)W:表示有效工作日(周一到周五),只能出现在DayofMonth域,系统将在离指定日期的最近的有效工作日触发事件。例如:在 DayofMonth使用5W,如果5日是星期六,则将在最近的工作日:星期五,即4日触发。如果5日是星期天,则在6日(周一)触发;如果5日在星期一到星期五中的一天,则就在5日触发。另外一点,W的最近寻找不会跨过月份 。
(8)LW:这两个字符可以连用,表示在某个月最后一个工作日,即最后一个星期五。
(9)#:用于确定每个月第几个星期几,只能出现在DayofMonth域。例如在4#2,表示某月的第二个星期三。
4、常用表达式例子
cron在线生成和例子https://www.bejson.com/othertools/cron/
(1)0 0 2 1 * ? * 表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI 表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006 表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0 10,14,16 * * ? 每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED 表示每个星期三中午12点
(7)0 0 12 * * ? 每天中午12点触发
(8)0 15 10 ? * * 每天上午10:15触发
(9)0 15 10 * * ? 每天上午10:15触发
(10)0 15 10 * * ? * 每天上午10:15触发
(11)0 15 10 * * ? 2005 2005年的每天上午10:15触发
(12)0 * 14 * * ? 在每天下午2点到下午2:59期间的每1分钟触发
(13)0 0/5 14 * * ? 在每天下午2点到下午2:55期间的每5分钟触发
(14)0 0/5 14,18 * * ? 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(15)0 0-5 14 * * ? 在每天下午2点到下午2:05期间的每1分钟触发
(16)0 10,44 14 ? 3 WED 每年三月的星期三的下午2:10和2:44触发
(17)0 15 10 ? * MON-FRI 周一至周五的上午10:15触发
(18)0 15 10 15 * ? 每月15日上午10:15触发
(19)0 15 10 L * ? 每月最后一日的上午10:15触发
(20)0 15 10 ? * 6L 每月的最后一个星期五上午10:15触发
(21)0 15 10 ? * 6L 2002-2005 2002年至2005年的每月的最后一个星期五上午10:15触发
(22)0 15 10 ? * 6#3 每月的第三个星期五上午10:15触发
5、cron生成代码
5.1 cron表达式生成工具类
package com.shucha.digitalportalbackstage.biz.utils;
import com.shucha.digitalportalbackstage.biz.model.TaskScheduleModel;
/**
* @author tqf
* @Description
* @Version 1.0
* @since 2022-03-14 11:07
*/
public class CronUtils {
/**
*
*方法摘要:构建Cron表达式
*@param taskScheduleModel
*@return String
*/
public static String createCronExpression(TaskScheduleModel taskScheduleModel){
StringBuffer cronExp = new StringBuffer("");
if(null == taskScheduleModel.getJobType()) {
System.out.println("执行周期未配置" );//执行周期未配置
}
if (null != taskScheduleModel.getSecond()
&& null == taskScheduleModel.getMinute()
&& null == taskScheduleModel.getHour()){
//每隔几秒
if (taskScheduleModel.getJobType().intValue() == 0) {
cronExp.append("0/").append(taskScheduleModel.getSecond());
cronExp.append(" ");
cronExp.append("* ");
cronExp.append("* ");
cronExp.append("* ");
cronExp.append("* ");
cronExp.append("?");
}
}
if (null != taskScheduleModel.getSecond()
&& null != taskScheduleModel.getMinute()
&& null == taskScheduleModel.getHour()){
//每隔几分钟
if (taskScheduleModel.getJobType().intValue() == 4) {
cronExp.append("* ");
cronExp.append("0/").append(taskScheduleModel.getMinute());
cronExp.append(" ");
cronExp.append("* ");
cronExp.append("* ");
cronExp.append("* ");
cronExp.append("?");
}
}
if (null != taskScheduleModel.getSecond()
&& null != taskScheduleModel.getMinute()
&& null != taskScheduleModel.getHour()) {
//秒
cronExp.append(taskScheduleModel.getSecond()).append(" ");
//分
cronExp.append(taskScheduleModel.getMinute()).append(" ");
//小时
cronExp.append(taskScheduleModel.getHour()).append(" ");
//每天
if(taskScheduleModel.getJobType().intValue() == 1){
cronExp.append("* ");//日
cronExp.append("* ");//月
cronExp.append("?");//周
}
//按每周
else if(taskScheduleModel.getJobType().intValue() == 3){
//一个月中第几天
cronExp.append("? ");
//月份
cronExp.append("* ");
//周
Integer[] weeks = taskScheduleModel.getDayOfWeeks();
for(int i = 0; i < weeks.length; i++){
if(i == 0){
cronExp.append(weeks[i]);
} else{
cronExp.append(",").append(weeks[i]);
}
}
}
//按每月
else if(taskScheduleModel.getJobType().intValue() == 2){
//一个月中的哪几天
Integer[] days = taskScheduleModel.getDayOfMonths();
for(int i = 0; i < days.length; i++){
if(i == 0){
cronExp.append(days[i]);
} else{
cronExp.append(",").append(days[i]);
}
}
//月份
cronExp.append(" * ");
//周
cronExp.append("?");
}
}
else {
System.out.println("时或分或秒参数未配置" );//时或分或秒参数未配置
}
return cronExp.toString();
}
/**
*
*方法摘要:生成计划的详细描述
*@param taskScheduleModel
*@return String
*/
public static String createDescription(TaskScheduleModel taskScheduleModel){
StringBuffer description = new StringBuffer("");
//计划执行开始时间
// Date startTime = taskScheduleModel.getScheduleStartTime();
if (null != taskScheduleModel.getSecond()
&& null != taskScheduleModel.getMinute()
&& null != taskScheduleModel.getHour()) {
//按每天
if(taskScheduleModel.getJobType().intValue() == 1){
description.append("每天");
description.append(taskScheduleModel.getHour()).append("时");
description.append(taskScheduleModel.getMinute()).append("分");
description.append(taskScheduleModel.getSecond()).append("秒");
description.append("执行");
}
//按每周
else if(taskScheduleModel.getJobType().intValue() == 3){
if(taskScheduleModel.getDayOfWeeks() != null && taskScheduleModel.getDayOfWeeks().length > 0) {
String days = "";
for(int i : taskScheduleModel.getDayOfWeeks()) {
days += "周" + i;
}
description.append("每周的").append(days).append(" ");
}
if (null != taskScheduleModel.getSecond()
&& null != taskScheduleModel.getMinute()
&& null != taskScheduleModel.getHour()) {
description.append(",");
description.append(taskScheduleModel.getHour()).append("时");
description.append(taskScheduleModel.getMinute()).append("分");
description.append(taskScheduleModel.getSecond()).append("秒");
}
description.append("执行");
}
//按每月
else if(taskScheduleModel.getJobType().intValue() == 2){
//选择月份
if(taskScheduleModel.getDayOfMonths() != null && taskScheduleModel.getDayOfMonths().length > 0) {
String days = "";
for(int i : taskScheduleModel.getDayOfMonths()) {
days += i + "号";
}
description.append("每月的").append(days).append(" ");
}
description.append(taskScheduleModel.getHour()).append("时");
description.append(taskScheduleModel.getMinute()).append("分");
description.append(taskScheduleModel.getSecond()).append("秒");
description.append("执行");
}
}
return description.toString();
}
//参考例子
public static void main(String[] args) {
// 每月的1号2号 10时12分0秒执行
Integer[] dayOfMonths = new Integer[2];
dayOfMonths[0] = 1;
dayOfMonths[1] = 2;
TaskScheduleModel taskScheduleModel = new TaskScheduleModel();
taskScheduleModel.setJobType(2)
.setSecond(0)
.setDayOfMonths(dayOfMonths)
.setHour(10)
.setMinute(12);
String cropExp = createCronExpression(taskScheduleModel);
System.out.println(cropExp + ":" + createDescription(taskScheduleModel));
// 每周的周1周2周3 10时12分0秒执行
TaskScheduleModel weekModel = new TaskScheduleModel();
Integer[] dayOfWeeks = new Integer[3];
dayOfWeeks[0] = 1;
dayOfWeeks[1] = 2;
dayOfWeeks[2] = 3;
weekModel.setJobType(3)
.setSecond(0)
.setDayOfWeeks(dayOfWeeks)
.setHour(10)
.setMinute(12);
cropExp = createCronExpression(weekModel);
System.out.println(cropExp + ":" + createDescription(weekModel));
// 每天10时12分0秒执行
TaskScheduleModel dayModel = new TaskScheduleModel();
Integer[] dayOfMonths1 = new Integer[2];
dayOfMonths1[0] = 1;
dayOfMonths1[1] = 2;
dayModel.setJobType(1)
.setSecond(0)
.setDayOfMonths(dayOfMonths1)
.setHour(10)
.setMinute(12);
cropExp = createCronExpression(dayModel);
System.out.println(cropExp + ":" + createDescription(dayModel));
}
}
5.2corn表达生成实体类
package com.shucha.digitalportalbackstage.biz.model;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/**
* @author tqf
* @Description
* @Version 1.0
* @since 2022-03-14 11:09
*/
@Data
@ApiModel(description = "cron表达体")
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = true)
public class TaskScheduleModel extends Model<TaskScheduleModel> {
/**
* 所选作业类型:
* 1 -> 每天
* 2 -> 每月
* 3 -> 每周
*/
@ApiModelProperty(value = "所选作业类型(1-每天,2-每月,3-每周)")
Integer jobType;
/**一周的哪几天*/
@ApiModelProperty(value = "一周的哪几天")
Integer[] dayOfWeeks;
/**一个月的哪几天*/
@ApiModelProperty(value = "一个月的哪几天")
Integer[] dayOfMonths;
/**秒 */
@ApiModelProperty(value = "秒")
Integer second;
/**分 */
@ApiModelProperty(value = "分")
Integer minute;
/**时 */
@ApiModelProperty(value = "时")
Integer hour;
}