用法和看效果:
1. 使用方法
package com.zlyx.easy.start.test;
import com.zlyx.easy.start.utils.CronUtil.DayCron;
import com.zlyx.easy.start.utils.CronUtil.MonthCron;
import com.zlyx.easy.start.utils.CronUtil.WeekCron;
public class CronTest {
public static void main(String[] args) {
DayCron.cron(1, 0, 30);
WeekCron.cron(1, 0, 30).day(1, 2, 3);
MonthCron.cron(1, 0, 30).day(1, 2, 3);
}
}
2. 運行效果
30 0 1 * * ?
每天1時0分30秒執行
30 0 1 ? * 1,2,3
每週的周1周2周3 1時0分30秒執行
30 0 1 1,2,3 * ?
每月的1號2號3號 1時0分30秒執行
3.在線驗證:
4. 工具類源碼
package com.zlyx.easy.start.utils;
/**
*
* Cron表達式生成工具
*
*/
public class CronUtil {
/**
* 執行方式
*
*/
public enum ExecuteType {
EveryDay, EveryWeek, EveryMonth;
}
public static class DayCron extends BaseCron {
public DayCron(Integer hour, Integer minute, Integer second) {
super(ExecuteType.EveryDay, hour, minute, second);
}
public static String cron(Integer hour, Integer minute, Integer second) {
return new DayCron(hour, minute, second).toString();
}
}
public static class WeekCron extends BaseCron {
public WeekCron(Integer hour, Integer minute, Integer second) {
super(ExecuteType.EveryWeek, hour, minute, second);
}
/**
* 一週的哪幾天
*/
public String day(Integer... dayOfWeeks) {
this.dayOfWeeks = dayOfWeeks;
return toString();
}
public static WeekCron cron() {
return cron(0, 0, 0);
}
public static WeekCron cron(Integer hour, Integer minute, Integer second) {
return new WeekCron(hour, minute, second);
}
}
public static class MonthCron extends BaseCron {
public MonthCron(Integer hour, Integer minute, Integer second) {
super(ExecuteType.EveryMonth, hour, minute, second);
}
/**
* 一個月的哪幾天
*/
public String day(Integer... dayOfMonths) {
this.dayOfMonths = dayOfMonths;
return toString();
}
public static MonthCron cron() {
return cron(0, 0, 0);
}
public static MonthCron cron(Integer hour, Integer minute, Integer second) {
return new MonthCron(hour, minute, second);
}
}
/**
* 表達式
*/
public static class BaseCron {
/**
* 表達式
*/
private StringBuffer cronExp;
/***
* 描述
*/
private StringBuffer description;
/**
* 執行間隔
*/
ExecuteType executeType;
/**
* 一週的哪幾天
*/
Integer[] dayOfWeeks;
/**
* 一個月的哪幾天
*/
Integer[] dayOfMonths;
/** 秒 */
Integer second = 0;
/** 分 */
Integer minute = 0;
/** 時 */
Integer hour = 0;
/**
* 執行間隔
*
* @param executeType
*/
private BaseCron(ExecuteType executeType, Integer hour, Integer minute, Integer second) {
this.executeType = executeType;
this.second = second;
this.minute = minute;
this.hour = hour;
}
/**
*
* 構建Cron表達式
*
* @param taskScheduleModel
* @return String
*/
private String cronExp() {
cronExp = new StringBuffer("");
cronExp.append(second).append(" ");// 秒
cronExp.append(minute).append(" "); // 分
cronExp.append(hour).append(" "); // 小時
if (executeType == ExecuteType.EveryDay) {
cronExp.append("* ");// 日
cronExp.append("* ");// 月
cronExp.append("?");// 周
} else if (executeType == ExecuteType.EveryWeek) {
cronExp.append("? ");// 一個月中第幾天
cronExp.append("* ");// 月份
// 周
for (int i = 0; i < dayOfWeeks.length; i++) {
if (i == 0) {
cronExp.append(dayOfWeeks[i]);
} else {
cronExp.append(",").append(dayOfWeeks[i]);
}
}
} else if (executeType == ExecuteType.EveryMonth) {
for (int i = 0; i < dayOfMonths.length; i++) {
if (i == 0) {
cronExp.append(dayOfMonths[i]);
} else {
cronExp.append(",").append(dayOfMonths[i]);
}
}
cronExp.append(" * ");// 月份
cronExp.append("?");// 周
}
return cronExp.toString();
}
/**
* 表達式描述
*
* @return
*/
private String description() {
description = new StringBuffer("");
if (executeType == ExecuteType.EveryDay) {
description.append("每天");
description.append(hour).append("時");
description.append(minute).append("分");
description.append(second).append("秒");
description.append("執行");
} else if (executeType == ExecuteType.EveryWeek) {
if (dayOfWeeks != null && dayOfWeeks.length > 0) {
String days = "";
for (int i : dayOfWeeks) {
days += "周" + i;
}
description.append("每週的").append(days);
}
description.append(" ");
description.append(hour).append("時");
description.append(minute).append("分");
description.append(second).append("秒");
description.append("執行");
} else if (executeType == ExecuteType.EveryMonth) {
if (dayOfMonths != null && dayOfMonths.length > 0) {
String days = "";
for (int i : dayOfMonths) {
days += i + "號";
}
description.append("每月的").append(days).append(" ");
}
description.append(hour).append("時");
description.append(minute).append("分");
description.append(second).append("秒");
description.append("執行");
}
return description.toString();
}
public String build() throws Exception {
if (cronExp == null || cronExp.length() == 0) {
if (hour < 0 || hour > 23) {
throw new Exception("小時值必須在0~23之間");
}
if (minute < 0 || minute > 59) {
throw new Exception("分鐘值必須在0~59之間");
}
if (second < 0 || second > 59) {
throw new Exception("秒值必須在0~59之間");
}
System.out.println(cronExp());// 生成表達式
System.out.println(description()); // 生成描述
}
return cronExp.toString();
}
public String toString() {
try {
return build();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
5. 參考博客: