Class CronExpression
java.lang.Object
cloud.opencode.base.date.cron.CronExpression
- All Implemented Interfaces:
Serializable
A cron expression parser and scheduler
Cron表达式解析器和调度器
This class parses and evaluates cron expressions. It supports the standard 5-field cron format (minute, hour, day of month, month, day of week).
此类解析和计算cron表达式。支持标准的5字段cron格式 (分钟、小时、日期、月份、星期几)。
Features | 主要功能:
- Parse standard 5-field cron expressions - 解析标准5字段cron表达式
- Support wildcards, ranges, lists, and step values - 支持通配符、范围、列表和步进值
- Calculate next execution time from a given date-time - 计算给定日期时间后的下次执行时间
- Match a date-time against the expression - 将日期时间与表达式匹配
Cron Expression Format | Cron表达式格式:
┌───────────── minute (0 - 59) │ ┌───────────── hour (0 - 23) │ │ ┌───────────── day of month (1 - 31) │ │ │ ┌───────────── month (1 - 12) │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday) │ │ │ │ │ * * * * *
Special Characters | 特殊字符:
- * - any value | 任意值
- , - value list separator | 值列表分隔符
- - - range of values | 值范围
- / - step values | 步进值
Usage Examples | 使用示例:
// Every minute
CronExpression cron = CronExpression.parse("* * * * *");
// Every day at 8:30
CronExpression daily = CronExpression.parse("30 8 * * *");
// Get next execution time
LocalDateTime next = cron.nextExecution();
Security | 安全性:
- Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
- Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)
- Since:
- JDK 25, opencode-base-date V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringEvery day at midnight 每天午夜static final StringEvery hour 每小时static final StringEvery minute 每分钟static final StringFirst day of every month at midnight 每月第一天午夜static final StringEvery Monday at midnight 每周一午夜static final StringFirst day of every year at midnight 每年第一天午夜 -
Method Summary
Modifier and TypeMethodDescriptionstatic CronExpressiondaily(int hour, int minute) Creates a cron expression for a specific time 为特定时间创建cron表达式describe()Gets a human-readable description of the cron expression 获取cron表达式的人类可读描述booleanstatic CronExpressioneveryHours(int hours) Creates a cron expression for every N hours 为每N小时创建cron表达式static CronExpressioneveryMinutes(int minutes) Creates a cron expression for every N minutes 为每N分钟创建cron表达式Gets the allowed days of month 获取允许的日期Gets the allowed days of week 获取允许的星期几Gets the original expression string 获取原始表达式字符串getHours()Gets the allowed hours 获取允许的小时Gets the allowed minutes 获取允许的分钟Gets the allowed months 获取允许的月份inthashCode()booleanmatches(LocalDateTime dateTime) Checks if the expression matches the specified date-time 检查表达式是否匹配指定的日期时间Gets the next execution time after now 获取当前之后的下一次执行时间nextExecution(LocalDateTime after) Gets the next execution time after the specified time 获取指定时间之后的下一次执行时间nextExecutions(int count) Gets the next N execution times 获取接下来的N次执行时间nextExecutions(LocalDateTime after, int count) Gets the next N execution times after the specified time 获取指定时间之后的N次执行时间static CronExpressionParses a cron expression 解析cron表达式Gets the previous execution time before now 获取当前之前的上一次执行时间previousExecution(LocalDateTime before) Gets the previous execution time before the specified time 获取指定时间之前的上一次执行时间toString()
-
Field Details
-
EVERY_MINUTE
-
EVERY_HOUR
-
DAILY
-
WEEKLY
-
MONTHLY
-
YEARLY
-
-
Method Details
-
parse
Parses a cron expression 解析cron表达式- Parameters:
expression- the cron expression | cron表达式- Returns:
- the CronExpression | CronExpression
- Throws:
OpenDateException- if the expression is invalid | 如果表达式无效则抛出异常
-
daily
Creates a cron expression for a specific time 为特定时间创建cron表达式- Parameters:
hour- the hour (0-23) | 小时minute- the minute (0-59) | 分钟- Returns:
- the CronExpression | CronExpression
-
everyMinutes
Creates a cron expression for every N minutes 为每N分钟创建cron表达式- Parameters:
minutes- the interval in minutes | 分钟间隔- Returns:
- the CronExpression | CronExpression
-
everyHours
Creates a cron expression for every N hours 为每N小时创建cron表达式- Parameters:
hours- the interval in hours | 小时间隔- Returns:
- the CronExpression | CronExpression
-
nextExecution
Gets the next execution time after now 获取当前之后的下一次执行时间- Returns:
- the next execution time | 下一次执行时间
-
nextExecution
Gets the next execution time after the specified time 获取指定时间之后的下一次执行时间- Parameters:
after- the starting time | 开始时间- Returns:
- the next execution time | 下一次执行时间
-
previousExecution
Gets the previous execution time before now 获取当前之前的上一次执行时间- Returns:
- the previous execution time | 上一次执行时间
-
previousExecution
Gets the previous execution time before the specified time 获取指定时间之前的上一次执行时间- Parameters:
before- the ending time | 结束时间- Returns:
- the previous execution time | 上一次执行时间
-
nextExecutions
Gets the next N execution times 获取接下来的N次执行时间- Parameters:
count- the number of executions | 执行次数- Returns:
- the list of execution times | 执行时间列表
-
nextExecutions
Gets the next N execution times after the specified time 获取指定时间之后的N次执行时间- Parameters:
after- the starting time | 开始时间count- the number of executions | 执行次数- Returns:
- the list of execution times | 执行时间列表
-
matches
Checks if the expression matches the specified date-time 检查表达式是否匹配指定的日期时间- Parameters:
dateTime- the date-time to check | 要检查的日期时间- Returns:
- true if matches | 如果匹配返回true
-
getExpression
Gets the original expression string 获取原始表达式字符串- Returns:
- the expression | 表达式
-
getMinutes
-
getHours
-
getDaysOfMonth
-
getMonths
-
getDaysOfWeek
-
describe
Gets a human-readable description of the cron expression 获取cron表达式的人类可读描述- Returns:
- the description | 描述
-
equals
-
hashCode
-
toString
-