Class CronUtil

java.lang.Object
cloud.opencode.base.date.cron.CronUtil

public final class CronUtil extends Object
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 Details

  • Method Details

    • isValid

      public static boolean isValid(String expression)
      Checks if a Cron expression is valid 检查Cron表达式是否有效
      Parameters:
      expression - the Cron expression | Cron表达式
      Returns:
      true if valid | 如果有效返回true
    • validate

      public static void validate(String expression)
      Validates a Cron expression and throws if invalid 验证Cron表达式,如果无效则抛出异常
      Parameters:
      expression - the Cron expression | Cron表达式
      Throws:
      IllegalArgumentException - if invalid | 如果无效则抛出异常
    • getNextExecutionTime

      public static Optional<LocalDateTime> getNextExecutionTime(String expression)
      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

      public static Optional<LocalDateTime> getNextExecutionTime(String expression, LocalDateTime after)
      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

      public static List<LocalDateTime> getNextExecutionTimes(String expression, int count)
      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

      public static Optional<ZonedDateTime> getNextExecutionTime(String expression, ZoneId zone)
      Gets the next execution time in a specific timezone 获取特定时区的下次执行时间
      Parameters:
      expression - the Cron expression | Cron表达式
      zone - the timezone | 时区
      Returns:
      the next execution time | 下次执行时间
    • getNextExecutionTime

      public static Optional<ZonedDateTime> getNextExecutionTime(String expression, ZonedDateTime after)
      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

      public static String describe(String expression)
      Gets a human-readable description of a Cron expression 获取Cron表达式的可读描述
      Parameters:
      expression - the Cron expression | Cron表达式
      Returns:
      the description | 描述
    • describeInChinese

      public static String describeInChinese(String expression)
      Gets a Chinese description of a Cron expression 获取Cron表达式的中文描述
      Parameters:
      expression - the Cron expression | Cron表达式
      Returns:
      the Chinese description | 中文描述
    • dailyAt

      public static String dailyAt(int hour, int minute)
      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

      public static String weekdaysAt(int hour, int minute)
      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

      public static String everyMinutes(int minutes)
      Creates a Cron expression for running every N minutes 创建每N分钟运行的Cron表达式
      Parameters:
      minutes - the interval in minutes | 分钟间隔
      Returns:
      the Cron expression | Cron表达式
    • everyHours

      public static String everyHours(int hours)
      Creates a Cron expression for running every N hours 创建每N小时运行的Cron表达式
      Parameters:
      hours - the interval in hours | 小时间隔
      Returns:
      the Cron expression | Cron表达式