Class CronUtil
java.lang.Object
cloud.opencode.base.date.cron.CronUtil
Utility class for Cron expression operations
Cron表达式工具类
This class provides static methods for working with Cron expressions, including validation, parsing, and calculating next execution times.
此类提供处理Cron表达式的静态方法,包括验证、解析和计算下次执行时间。
Features | 主要功能:
- Validate Cron expressions - 验证Cron表达式
- Calculate next execution time - 计算下次执行时间
- Get multiple upcoming executions - 获取多个即将执行的时间
- Common Cron expression constants - 常用Cron表达式常量
Usage Examples | 使用示例:
// Check if expression is valid
boolean valid = CronUtil.isValid("0 0 * * * ?");
// Get next execution time
Optional<LocalDateTime> next = CronUtil.getNextExecutionTime("0 0 12 * * ?");
// Get next 5 execution times
List<LocalDateTime> nextFive = CronUtil.getNextExecutionTimes("0 0 9 * * MON-FRI", 5);
Security | 安全性:
- Thread-safe: Yes - 线程安全: 是
- Null-safe: Yes (with explicit null checks) - 空值安全: 是(有明确的空值检查)
Performance | 性能特性:
- Time complexity: O(n) for getNextExecutionTimes where n=count; O(1) for single next-time lookup - 时间复杂度: getNextExecutionTimes 为 O(n),n 为查询次数;单次查询为 O(1)
- Space complexity: O(n) for result list in getNextExecutionTimes; O(1) otherwise - 空间复杂度: getNextExecutionTimes 结果列表为 O(n);其余为 O(1)
- 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 midnightstatic final StringEvery day at noonstatic final StringEvery 15 minutesstatic final StringEvery 30 minutesstatic final StringEvery 5 minutesstatic final StringEvery hourstatic final StringEvery minutestatic final StringEvery secondstatic final StringFirst day of every month at midnightstatic final StringEvery weekday (Monday to Friday) at 9 AMstatic final StringEvery Monday at midnight -
Method Summary
Modifier and TypeMethodDescriptionstatic StringdailyAt(int hour, int minute) Creates a Cron expression for running at a specific time every day 创建每天在特定时间运行的Cron表达式static StringGets a human-readable description of a Cron expression 获取Cron表达式的可读描述static StringdescribeInChinese(String expression) Gets a Chinese description of a Cron expression 获取Cron表达式的中文描述static StringeveryHours(int hours) Creates a Cron expression for running every N hours 创建每N小时运行的Cron表达式static StringeveryMinutes(int minutes) Creates a Cron expression for running every N minutes 创建每N分钟运行的Cron表达式static Optional<LocalDateTime> getNextExecutionTime(String expression) Gets the next execution time for a Cron expression 获取Cron表达式的下次执行时间static Optional<LocalDateTime> getNextExecutionTime(String expression, LocalDateTime after) Gets the next execution time after a specific time 获取指定时间之后的下次执行时间static Optional<ZonedDateTime> getNextExecutionTime(String expression, ZonedDateTime after) Gets the next execution time after a specific zoned time 获取指定时区时间之后的下次执行时间static Optional<ZonedDateTime> getNextExecutionTime(String expression, ZoneId zone) Gets the next execution time in a specific timezone 获取特定时区的下次执行时间static List<LocalDateTime> getNextExecutionTimes(String expression, int count) Gets multiple next execution times 获取多个下次执行时间static List<LocalDateTime> getNextExecutionTimes(String expression, LocalDateTime after, int count) Gets multiple next execution times after a specific time 获取指定时间之后的多个执行时间static booleanChecks if a Cron expression is valid 检查Cron表达式是否有效static voidValidates a Cron expression and throws if invalid 验证Cron表达式,如果无效则抛出异常static StringweekdaysAt(int hour, int minute) Creates a Cron expression for running at a specific time on weekdays 创建工作日在特定时间运行的Cron表达式
-
Field Details
-
EVERY_SECOND
-
EVERY_MINUTE
-
EVERY_HOUR
-
DAILY_MIDNIGHT
-
DAILY_NOON
-
WEEKLY_MONDAY
-
MONTHLY_FIRST
-
WEEKDAYS_9AM
-
EVERY_5_MINUTES
-
EVERY_15_MINUTES
-
EVERY_30_MINUTES
-
-
Method Details
-
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 and throws if invalid 验证Cron表达式,如果无效则抛出异常- Parameters:
expression- the Cron expression | Cron表达式- Throws:
IllegalArgumentException- if invalid | 如果无效则抛出异常
-
getNextExecutionTime
Gets the next execution time for a Cron expression 获取Cron表达式的下次执行时间- Parameters:
expression- the Cron expression | Cron表达式- Returns:
- the next execution time, or empty if not calculable | 下次执行时间,如果无法计算则为空
-
getNextExecutionTime
Gets the next execution time after a specific time 获取指定时间之后的下次执行时间- Parameters:
expression- the Cron expression | Cron表达式after- the reference time | 参考时间- Returns:
- the next execution time, or empty if not calculable | 下次执行时间,如果无法计算则为空
-
getNextExecutionTimes
Gets multiple next execution times 获取多个下次执行时间- Parameters:
expression- the Cron expression | Cron表达式count- the number of times to get | 要获取的次数- Returns:
- list of execution times | 执行时间列表
-
getNextExecutionTimes
public static List<LocalDateTime> getNextExecutionTimes(String expression, LocalDateTime after, int count) Gets multiple next execution times after a specific time 获取指定时间之后的多个执行时间- Parameters:
expression- the Cron expression | Cron表达式after- the reference time | 参考时间count- the number of times to get | 要获取的次数- Returns:
- list of execution times | 执行时间列表
-
getNextExecutionTime
Gets the next execution time in a specific timezone 获取特定时区的下次执行时间- Parameters:
expression- the Cron expression | Cron表达式zone- the timezone | 时区- Returns:
- the next execution time | 下次执行时间
-
getNextExecutionTime
Gets the next execution time after a specific zoned time 获取指定时区时间之后的下次执行时间- Parameters:
expression- the Cron expression | Cron表达式after- the reference time | 参考时间- Returns:
- the next execution time | 下次执行时间
-
describe
-
describeInChinese
-
dailyAt
Creates a Cron expression for running at a specific time every day 创建每天在特定时间运行的Cron表达式- Parameters:
hour- the hour (0-23) | 小时(0-23)minute- the minute (0-59) | 分钟(0-59)- Returns:
- the Cron expression | Cron表达式
-
weekdaysAt
Creates a Cron expression for running at a specific time on weekdays 创建工作日在特定时间运行的Cron表达式- Parameters:
hour- the hour (0-23) | 小时(0-23)minute- the minute (0-59) | 分钟(0-59)- Returns:
- the Cron expression | Cron表达式
-
everyMinutes
Creates a Cron expression for running every N minutes 创建每N分钟运行的Cron表达式- Parameters:
minutes- the interval in minutes | 分钟间隔- Returns:
- the Cron expression | Cron表达式
-
everyHours
Creates a Cron expression for running every N hours 创建每N小时运行的Cron表达式- Parameters:
hours- the interval in hours | 小时间隔- Returns:
- the Cron expression | Cron表达式
-