From 10c52408b28ecdf5e6e7f349ffda468160564fdb Mon Sep 17 00:00:00 2001 From: lzq <2495532633@qq.com> Date: Wed, 24 Dec 2025 00:29:17 +0800 Subject: [PATCH 1/2] 1 --- .../common/sichen/support/CronExpression.java | 168 +++++++----------- 1 file changed, 60 insertions(+), 108 deletions(-) diff --git a/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/support/CronExpression.java b/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/support/CronExpression.java index e51be99..274c1ea 100644 --- a/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/support/CronExpression.java +++ b/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/support/CronExpression.java @@ -19,173 +19,132 @@ package com.njzscloud.common.sichen.support; import java.text.ParseException; import java.util.*; - /** - * Provides a parser and evaluator for unix-like cron expressions. Cron - * expressions provide the ability to specify complex time combinations such as - * "At 8:00am every Monday through Friday" or "At 1:30am every - * last Friday of the month". + * 提供类 Unix 风格 Cron 表达式的解析器和求值器。Cron 表达式能够指定复杂的时间组合, + * 例如“每周一至周五的上午 8:00”或“每月最后一个周五的凌晨 1:30”。 + * *
- * Cron expressions are comprised of 6 required fields and one optional field - * separated by white space. The fields respectively are described as follows: + * Cron 表达式由 6 个必填字段和 1 个可选字段组成,字段之间通过空格分隔。各字段的详细说明如下: * *
| Field Name | + *字段名称 | *- * | Allowed Values | + *允许值 | *- * | Allowed Special Characters | + *允许的特殊字符 | *
|---|---|---|---|---|---|---|---|
Seconds |
+ * 秒(Seconds) |
* * | 0-59 |
* * | , - * / |
* ||
Minutes |
+ * 分(Minutes) |
* * | 0-59 |
* * | , - * / |
* ||
Hours |
+ * 时(Hours) |
* * | 0-23 |
* * | , - * / |
* ||
Day-of-month |
+ * 日(Day-of-month) |
* * | 1-31 |
* * | , - * ? / L W |
* ||
Month |
+ * 月(Month) |
* - * | 0-11 or JAN-DEC |
+ * 0-11 或 JAN-DEC(一月至十二月) |
* * | , - * / |
* |
Day-of-Week |
+ * 周(Day-of-Week) |
* - * | 1-7 or SUN-SAT |
+ * 1-7 或 SUN-SAT(周日至周六) |
* * | , - * ? / L # |
* |
Year (Optional) |
+ * 年(Year,可选) |
* - * | empty, 1970-2199 |
+ * 空值、1970-2199 |
* * | , - * / |
*
- * The '*' character is used to specify all values. For example, "*" - * in the minute field means "every minute". + * '*' 字符用于指定“所有可能的值”。例如,在“分钟”字段中使用 "*" 表示“每分钟”。 *
- * The '?' character is allowed for the day-of-month and day-of-week fields. It - * is used to specify 'no specific value'. This is useful when you need to - * specify something in one of the two fields, but not the other. + * '?' 字符仅允许在“日(Day-of-month)”和“周(Day-of-Week)”字段中使用, + * 用于表示“不指定具体值”。当你需要在这两个字段中的一个指定条件,而另一个无需指定时,该字符非常有用。 *
- * The '-' character is used to specify ranges For example "10-12" in - * the hour field means "the hours 10, 11 and 12". + * '-' 字符用于指定时间范围。例如,在“小时”字段中使用 "10-12" 表示“上午 10 点、11 点和中午 12 点”。 *
- * The ',' character is used to specify additional values. For example - * "MON,WED,FRI" in the day-of-week field means "the days Monday, - * Wednesday, and Friday". + * ',' 字符用于指定多个额外的取值。例如,在“周”字段中使用 "MON,WED,FRI" 表示“周一、周三和周五”。 *
- * The '/' character is used to specify increments. For example "0/15" - * in the seconds field means "the seconds 0, 15, 30, and 45". And - * "5/15" in the seconds field means "the seconds 5, 20, 35, and - * 50". Specifying '*' before the '/' is equivalent to specifying 0 is - * the value to start with. Essentially, for each field in the expression, there - * is a set of numbers that can be turned on or off. For seconds and minutes, - * the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to - * 31, and for months 0 to 11 (JAN to DEC). The "/" character simply helps you turn - * on every "nth" value in the given set. Thus "7/6" in the - * month field only turns on month "7", it does NOT mean every 6th - * month, please note that subtlety. + * '/' 字符用于指定递增步长。例如,在“秒”字段中使用 "0/15" 表示“第 0 秒、15 秒、30 秒和 45 秒”; + * 而 "5/15" 在“秒”字段中表示“第 5 秒、20 秒、35 秒和 50 秒”。在 '/' 前指定 '*' 等价于以 0 作为起始值。 + * 本质上,表达式的每个字段都对应一组可启用或禁用的数字集合:秒和分的范围是 0-59,小时是 0-23, + * 日是 1-31,月是 0-11(对应 JAN 至 DEC)。'/' 字符的作用是在指定集合中启用“每隔 n 个”的取值。 + * 需要注意一个细节:在“月”字段中使用 "7/6" 仅会启用“7 月”,并不表示“每 6 个月”。 *
- * The 'L' character is allowed for the day-of-month and day-of-week fields. - * This character is short-hand for "last", but it has different - * meaning in each of the two fields. For example, the value "L" in - * the day-of-month field means "the last day of the month" - day 31 - * for January, day 28 for February on non-leap years. If used in the - * day-of-week field by itself, it simply means "7" or - * "SAT". But if used in the day-of-week field after another value, it - * means "the last xxx day of the month" - for example "6L" - * means "the last friday of the month". You can also specify an offset - * from the last day of the month, such as "L-3" which would mean the third-to-last - * day of the calendar month. When using the 'L' option, it is important not to - * specify lists, or ranges of values, as you'll get confusing/unexpected results. + * 'L' 字符仅允许在“日(Day-of-month)”和“周(Day-of-Week)”字段中使用,是“last(最后一个)”的缩写, + * 但在两个字段中的含义有所不同。例如,在“日”字段中使用 "L" 表示“当月的最后一天”——1 月对应 31 日, + * 非闰年的 2 月对应 28 日。若在“周”字段中单独使用 "L",仅表示“7”或“SAT(周六)”; + * 若在其他值后使用,则表示“当月最后一个指定星期几”,例如 "6L" 表示“当月最后一个周五”。 + * 你也可以指定相对于月末的偏移量,例如 "L-3" 表示“当月的倒数第三天”。 + * 注意:使用 'L' 选项时,请勿同时指定多个取值(列表)或范围,否则会得到混乱或不符合预期的结果。 *
- * The 'W' character is allowed for the day-of-month field. This character - * is used to specify the weekday (Monday-Friday) nearest the given day. As an - * example, if you were to specify "15W" as the value for the - * day-of-month field, the meaning is: "the nearest weekday to the 15th of - * the month". So if the 15th is a Saturday, the trigger will fire on - * Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the - * 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th. - * However if you specify "1W" as the value for day-of-month, and the - * 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not - * 'jump' over the boundary of a month's days. The 'W' character can only be - * specified when the day-of-month is a single day, not a range or list of days. + * 'W' 字符仅允许在“日(Day-of-month)”字段中使用,用于指定“距离给定日期最近的工作日(周一至周五)”。 + * 例如,若在“日”字段中指定 "15W",含义是“当月 15 日附近最近的工作日”:如果 15 日是周六, + * 则触发时间为 14 日(周五);如果 15 日是周日,则触发时间为 16 日(周一);如果 15 日是周二, + * 则触发时间为 15 日(周二)本身。若指定 "1W" 且 1 日是周六,触发时间会是 3 日(周一), + * 因为该字符不会“跨越”月份的边界。'W' 字符仅能在“日”字段指定单个日期时使用, + * 不能用于日期范围或多个日期列表。 *
- * The 'L' and 'W' characters can also be combined for the day-of-month - * expression to yield 'LW', which translates to "last weekday of the - * month". + * 'L' 和 'W' 字符可在“日(Day-of-month)”字段中组合使用,形成 "LW",含义是“当月的最后一个工作日”。 *
- * The '#' character is allowed for the day-of-week field. This character is - * used to specify "the nth" XXX day of the month. For example, the - * value of "6#3" in the day-of-week field means the third Friday of - * the month (day 6 = Friday and "#3" = the 3rd one in the month). - * Other examples: "2#1" = the first Monday of the month and - * "4#5" = the fifth Wednesday of the month. Note that if you specify - * "#5" and there is not 5 of the given day-of-week in the month, then - * no firing will occur that month. If the '#' character is used, there can - * only be one expression in the day-of-week field ("3#1,6#3" is - * not valid, since there are two expressions). + * '#' 字符仅允许在“周(Day-of-Week)”字段中使用,用于指定“当月第 n 个星期几”。 + * 例如,在“周”字段中使用 "6#3" 表示“当月第三个周五”(6 对应周五,"#3" 对应第三个)。 + * 其他示例:"2#1" 表示“当月第一个周一”,"4#5" 表示“当月第五个周三”。 + * 注意:若指定了 "#5" 但当月不存在对应的第 5 个星期几,则该月不会触发任务。 + * 此外,使用 '#' 字符时,“周”字段中只能包含一个表达式(例如 "3#1,6#3" 是无效的,因为它包含两个表达式)。 *
- * + * *
- * The legal characters and the names of months and days of the week are not - * case sensitive. + * 所有合法字符以及月份、星期几的名称均不区分大小写。 * *
- * NOTES: + * 注意事项: *
- * Borrowed from quartz v2.3.1
+ * 源自 Quartz 框架 v2.3.1 版本
*/
public final class CronExpression {
@@ -1160,13 +1119,6 @@ public final class CronExpression {
return integer;
}
- ////////////////////////////////////////////////////////////////////////////
- //
- // Computation Functions
- //
-
- /// /////////////////////////////////////////////////////////////////////////
-
public Date getTimeAfter(Date afterTime) {
// Computation is based on Gregorian year only.
From 2a7743db5a9e5b4dc26bc8000283bd7ea792242f Mon Sep 17 00:00:00 2001
From: lzq <2495532633@qq.com>
Date: Wed, 24 Dec 2025 10:49:11 +0800
Subject: [PATCH 2/2] =?UTF-8?q?=E6=A3=80=E6=9F=A5=E5=B9=B6=E8=8E=B7?=
=?UTF-8?q?=E5=8F=96=E6=9C=80=E8=BF=91=E6=89=A7=E8=A1=8C=E6=97=B6=E9=97=B4?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../sichen/controller/TaskController.java | 30 +++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/controller/TaskController.java b/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/controller/TaskController.java
index a662ff4..941c7c9 100644
--- a/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/controller/TaskController.java
+++ b/njzscloud-common/njzscloud-common-sichen/src/main/java/com/njzscloud/common/sichen/controller/TaskController.java
@@ -1,7 +1,9 @@
package com.njzscloud.common.sichen.controller;
import cn.hutool.core.bean.BeanUtil;
+import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
+import com.njzscloud.common.core.ex.ExceptionMsg;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.core.utils.R;
import com.njzscloud.common.mp.support.PageParam;
@@ -11,6 +13,7 @@ import com.njzscloud.common.sichen.pojo.param.ModifyTaskParam;
import com.njzscloud.common.sichen.pojo.param.SearchTaskParam;
import com.njzscloud.common.sichen.pojo.param.SearchTaskResult;
import com.njzscloud.common.sichen.service.CombineStoreService;
+import com.njzscloud.common.sichen.support.CronExpression;
import com.njzscloud.common.sichen.support.TaskInfo;
import com.njzscloud.common.sichen.support.TaskStore;
import com.njzscloud.common.sichen.util.TaskUtil;
@@ -19,6 +22,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -96,6 +102,30 @@ public class TaskController {
}
}
+ /**
+ * 检查并获取最近执行时间
+ */
+ @GetMapping("/check_expression")
+ public R> detail(@RequestParam("expression") String expression) {
+ CronExpression cronExpression;
+ try {
+ cronExpression = new CronExpression(expression);
+ } catch (ParseException e) {
+ return R.failed(ExceptionMsg.CLI_ERR_MSG, "表达式错误:{}", e.getMessage());
+ }
+ Date timePoint = new Date();
+ int i = 0;
+ ArrayList