Class CronValidator

java.lang.Object
cloud.opencode.base.cron.CronValidator

public final class CronValidator extends Object
Cron Validator - Cron Expression Validation Utility Cron验证器 - Cron表达式验证工具类

Validates cron expressions for syntax correctness and optional minimum interval. Supports all syntax recognized by CronExpression including aliases, special characters (L, W, #), and macros.

验证Cron表达式的语法正确性和可选的最小间隔。 支持 CronExpression 识别的所有语法,包括别名、 特殊字符(L、W、#)和宏。

Features | 主要功能:

  • Syntax validation via parse - 通过解析进行语法验证
  • Minimum interval enforcement - 最小间隔强制检查
  • Estimated interval calculation - 预估间隔计算
  • Boolean isValid check (no exception) - 布尔isValid检查(无异常)

Usage Examples | 使用示例:

// Quick check | 快速检查
CronValidator.isValid("0 9 * * MON-FRI")  // true
CronValidator.isValid("invalid")           // false

// Validate with exception | 验证(抛异常)
CronValidator.validate("0 9 * * MON-FRI"); // OK

// Minimum interval check | 最小间隔检查
CronValidator.validate("* * * * * *", Duration.ofSeconds(5)); // throws

Security | 安全性:

  • Thread-safe: Yes (stateless static methods) - 线程安全: 是(无状态静态方法)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-cron V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • validate

      public static void validate(String expression)
      Validates a cron expression 验证Cron表达式
      Parameters:
      expression - the cron expression | Cron表达式
      Throws:
      OpenCronException - if invalid | 如果无效
    • validate

      public static void validate(String expression, Duration minInterval)
      Validates a cron expression with a minimum interval check 验证Cron表达式并检查最小间隔
      Parameters:
      expression - the cron expression | Cron表达式
      minInterval - the minimum interval between executions | 执行之间的最小间隔
      Throws:
      OpenCronException - if invalid or interval too short | 如果无效或间隔太短
    • 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
    • getEstimatedInterval

      public static Duration getEstimatedInterval(String expression)
      Gets the estimated interval between consecutive executions 获取连续执行之间的预估间隔
      Parameters:
      expression - the cron expression | Cron表达式
      Returns:
      the estimated interval | 预估间隔