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 longcountExecutionsBetween(String expression, ZonedDateTime from, ZonedDateTime to) Counts executions between two times 计算两个时间点之间的执行次数static StringGets a human-readable description of a cron expression 获取Cron表达式的人类可读描述static StringGets a human-readable description in the specified locale 获取指定语言的人类可读描述static List<ZonedDateTime> executionsBetween(String expression, ZonedDateTime from, ZonedDateTime to) Lists all executions between two times 列出两个时间点之间的所有执行时间static CronExplanationexplain(String expression, ZonedDateTime from) Gets a comprehensive explanation for debugging 获取用于调试的综合解释信息static DurationgetEstimatedInterval(String expression) Gets the estimated interval between executions 获取执行之间的预估间隔static booleanhasOverlap(String expr1, String expr2, ZonedDateTime from, ZonedDateTime to) Checks if two expressions overlap in a time range 检查两个表达式在时间范围内是否有重叠static booleanisEquivalent(String expr1, String expr2) Checks if two cron expressions produce the same schedule 检查两个Cron表达式是否产生相同的调度static booleanChecks if a cron expression is valid 检查Cron表达式是否有效static ZonedDateTimenextExecution(String expression, ZonedDateTime from) Gets the next execution time 获取下次执行时间static ZonedDateTimenextExecution(String expression, ZonedDateTime from, Predicate<ZonedDateTime> filter) Gets the next execution time that satisfies a filter 获取满足过滤条件的下次执行时间static List<ZonedDateTime> nextExecutions(String expression, ZonedDateTime from, int count) Gets the next N execution times 获取下N次执行时间static ZonedDateTimenextOverlap(String expr1, String expr2, ZonedDateTime from) Finds the next time both expressions fire simultaneously 查找两个表达式同时触发的下一个时间static CronExpressionParses a cron expression 解析Cron表达式static ZonedDateTimepreviousExecution(String expression, ZonedDateTime from) Gets the previous execution time 获取上次执行时间static ZonedDateTimepreviousExecution(String expression, ZonedDateTime from, Predicate<ZonedDateTime> filter) Gets the previous execution time that satisfies a filter 获取满足过滤条件的上次执行时间static List<ZonedDateTime> previousExecutions(String expression, ZonedDateTime from, int count) Gets the previous N execution times 获取前N次执行时间static Stream<ZonedDateTime> reverseStream(String expression, ZonedDateTime from) Returns a lazy stream of past execution times (newest first) 返回过去执行时间的惰性流(最新在前)static Stream<ZonedDateTime> stream(String expression, ZonedDateTime from) Returns a lazy stream of future execution times 返回未来执行时间的惰性流static DurationtimeFromLastExecution(String expression, ZonedDateTime from) Gets the duration since the last execution 获取距上次执行的时间间隔static DurationtimeToNextExecution(String expression, ZonedDateTime from) Gets the duration until the next execution 获取距下次执行的时间间隔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
-
timeToNextExecution
Gets the duration until the next execution 获取距下次执行的时间间隔- Parameters:
expression- the cron expression | Cron表达式from- the reference time | 参考时间- Returns:
- the duration to next execution, or null | 距下次执行的Duration,或null
-
timeFromLastExecution
Gets the duration since the last execution 获取距上次执行的时间间隔- Parameters:
expression- the cron expression | Cron表达式from- the reference time | 参考时间- Returns:
- the duration from last execution, or null | 距上次执行的Duration,或null
-
countExecutionsBetween
Counts executions between two times 计算两个时间点之间的执行次数- Parameters:
expression- the cron expression | Cron表达式from- the start time (exclusive) | 开始时间(不包含)to- the end time (inclusive) | 结束时间(包含)- Returns:
- the count of executions | 执行次数
-
executionsBetween
public static List<ZonedDateTime> executionsBetween(String expression, ZonedDateTime from, ZonedDateTime to) Lists all executions between two times 列出两个时间点之间的所有执行时间- Parameters:
expression- the cron expression | Cron表达式from- the start time (exclusive) | 开始时间(不包含)to- the end time (inclusive) | 结束时间(包含)- Returns:
- unmodifiable list of execution times | 不可修改的执行时间列表
-
isEquivalent
Checks if two cron expressions produce the same schedule 检查两个Cron表达式是否产生相同的调度- Parameters:
expr1- the first cron expression | 第一个Cron表达式expr2- the second cron expression | 第二个Cron表达式- Returns:
- true if equivalent schedules | 如果调度等价返回true
-
explain
Gets a comprehensive explanation for debugging 获取用于调试的综合解释信息- Parameters:
expression- the cron expression | Cron表达式from- the reference time | 参考时间- Returns:
- the explanation | 解释信息
-
stream
Returns a lazy stream of future execution times 返回未来执行时间的惰性流- Parameters:
expression- the cron expression | Cron表达式from- the start time (exclusive) | 开始时间(不包含)- Returns:
- an ordered stream of execution times | 有序的执行时间流
-
reverseStream
Returns a lazy stream of past execution times (newest first) 返回过去执行时间的惰性流(最新在前)- Parameters:
expression- the cron expression | Cron表达式from- the reference time (exclusive) | 参考时间(不包含)- Returns:
- an ordered stream of past execution times | 有序的过去执行时间流
-
nextExecution
public static ZonedDateTime nextExecution(String expression, ZonedDateTime from, Predicate<ZonedDateTime> filter) Gets the next execution time that satisfies a filter 获取满足过滤条件的下次执行时间- Parameters:
expression- the cron expression | Cron表达式from- the start time (exclusive) | 开始时间(不包含)filter- the filter predicate | 过滤谓词- Returns:
- the next matching execution time, or null | 下次匹配的执行时间
-
previousExecution
public static ZonedDateTime previousExecution(String expression, ZonedDateTime from, Predicate<ZonedDateTime> filter) Gets the previous execution time that satisfies a filter 获取满足过滤条件的上次执行时间- Parameters:
expression- the cron expression | Cron表达式from- the reference time (exclusive) | 参考时间(不包含)filter- the filter predicate | 过滤谓词- Returns:
- the previous matching execution time, or null | 上次匹配的执行时间
-
nextOverlap
Finds the next time both expressions fire simultaneously 查找两个表达式同时触发的下一个时间- Parameters:
expr1- the first cron expression | 第一个Cron表达式expr2- the second cron expression | 第二个Cron表达式from- the start time (exclusive) | 开始时间(不包含)- Returns:
- the next overlapping time, or null | 下一个重叠时间
-
hasOverlap
Checks if two expressions overlap in a time range 检查两个表达式在时间范围内是否有重叠- Parameters:
expr1- the first cron expression | 第一个Cron表达式expr2- the second cron expression | 第二个Cron表达式from- the start time (exclusive) | 开始时间(不包含)to- the end time (inclusive) | 结束时间(包含)- Returns:
- true if overlapping | 如果重叠返回true
-
describe
-
describe
-
builder
Creates a new cron expression builder 创建新的Cron表达式构建器- Returns:
- the builder | 构建器
-