Class OpenCron
java.lang.Object
cloud.opencode.base.cron.OpenCron
OpenCron - Cron Expression Facade
OpenCron - Cron表达式门面类
Unified entry point for cron expression parsing, validation, scheduling, and human-readable description. All cron functionality is accessible through this single facade class.
Cron表达式解析、验证、调度和人类可读描述的统一入口。 所有Cron功能都可通过此单一门面类访问。
Features | 主要功能:
- Parse cron expressions (5/6-field, macros, aliases) - 解析Cron表达式
- Validate expressions with optional minimum interval check - 验证表达式
- Calculate next/previous execution times - 计算下次/上次执行时间
- Generate human-readable descriptions - 生成人类可读描述
- Fluent builder API for constructing expressions - 流式构建器API
Usage Examples | 使用示例:
// Parse and query next execution | 解析并查询下次执行
ZonedDateTime next = OpenCron.nextExecution("0 9 * * MON-FRI", ZonedDateTime.now());
// Get next 5 executions | 获取下5次执行时间
List<ZonedDateTime> times = OpenCron.nextExecutions("30 10 * * *", ZonedDateTime.now(), 5);
// Validate expression | 验证表达式
boolean valid = OpenCron.isValid("0 0 L * *");
// Human-readable description | 人类可读描述
String desc = OpenCron.describe("0 9 * * MON-FRI"); // "At 09:00, Monday through Friday"
// Builder API | 构建器API
CronExpression expr = OpenCron.builder().weekdays().at(9, 0).build();
// Macro support | 宏支持
CronExpression daily = OpenCron.parse("@daily");
Security | 安全性:
- Thread-safe: Yes (stateless static methods) - 线程安全: 是(无状态静态方法)
- Null-safe: Yes (rejects null inputs) - 空值安全: 是
- Since:
- JDK 25, opencode-base-cron V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String@daily— Every day at midnight | 每天午夜static final String@hourly— Every hour | 每小时static final String@monthly— 1st of each month at midnight | 每月1号午夜static final String@weekly— Sunday at midnight | 每周日午夜static final String@yearly— January 1st at midnight | 每年1月1日午夜 -
Method Summary
Modifier and TypeMethodDescriptionstatic CronBuilderbuilder()Creates a new cron expression builder 创建新的Cron表达式构建器static StringGets a human-readable description of a cron expression 获取Cron表达式的人类可读描述static DurationgetEstimatedInterval(String expression) Gets the estimated interval between executions 获取执行之间的预估间隔static booleanChecks if a cron expression is valid 检查Cron表达式是否有效static ZonedDateTimenextExecution(String expression, ZonedDateTime from) Gets the next execution time 获取下次执行时间static List<ZonedDateTime> nextExecutions(String expression, ZonedDateTime from, int count) Gets the next N execution times 获取下N次执行时间static CronExpressionParses a cron expression 解析Cron表达式static ZonedDateTimepreviousExecution(String expression, ZonedDateTime from) Gets the previous execution time 获取上次执行时间static List<ZonedDateTime> previousExecutions(String expression, ZonedDateTime from, int count) Gets the previous N execution times 获取前N次执行时间static voidValidates a cron expression, throwing on failure 验证Cron表达式,失败时抛出异常static voidValidates a cron expression with minimum interval check 验证Cron表达式并检查最小间隔
-
Field Details
-
YEARLY
-
MONTHLY
-
WEEKLY
-
DAILY
-
HOURLY
-
-
Method Details
-
parse
Parses a cron expression 解析Cron表达式- Parameters:
expression- the cron expression (5/6-field or macro) | Cron表达式(5/6字段或宏)- Returns:
- the parsed expression | 解析后的表达式
- Throws:
OpenCronException- if invalid | 如果无效
-
isValid
Checks if a cron expression is valid 检查Cron表达式是否有效- Parameters:
expression- the cron expression | Cron表达式- Returns:
- true if valid | 如果有效返回true
-
validate
Validates a cron expression, throwing on failure 验证Cron表达式,失败时抛出异常- Parameters:
expression- the cron expression | Cron表达式- Throws:
OpenCronException- if invalid | 如果无效
-
validate
Validates a cron expression with minimum interval check 验证Cron表达式并检查最小间隔- Parameters:
expression- the cron expression | Cron表达式minInterval- the minimum interval | 最小间隔- Throws:
OpenCronException- if invalid or interval too short | 如果无效或间隔太短
-
nextExecution
Gets the next execution time 获取下次执行时间- Parameters:
expression- the cron expression | Cron表达式from- the start time | 开始时间- Returns:
- the next execution time, or null | 下次执行时间,或null
-
nextExecutions
Gets the next N execution times 获取下N次执行时间- Parameters:
expression- the cron expression | Cron表达式from- the start time | 开始时间count- the number of executions | 执行次数- Returns:
- the list of execution times | 执行时间列表
-
previousExecution
Gets the previous execution time 获取上次执行时间- Parameters:
expression- the cron expression | Cron表达式from- the reference time | 参考时间- Returns:
- the previous execution time, or null | 上次执行时间,或null
-
previousExecutions
public static List<ZonedDateTime> previousExecutions(String expression, ZonedDateTime from, int count) Gets the previous N execution times 获取前N次执行时间- Parameters:
expression- the cron expression | Cron表达式from- the reference time | 参考时间count- the number of executions | 执行次数- Returns:
- the list of execution times (newest first) | 执行时间列表(最新在前)
-
getEstimatedInterval
-
describe
-
builder
Creates a new cron expression builder 创建新的Cron表达式构建器- Returns:
- the builder | 构建器
-