Class OpenCron

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

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

  • Method Details

    • parse

      public static CronExpression parse(String expression)
      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

      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, throwing on failure 验证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 minimum interval check 验证Cron表达式并检查最小间隔
      Parameters:
      expression - the cron expression | Cron表达式
      minInterval - the minimum interval | 最小间隔
      Throws:
      OpenCronException - if invalid or interval too short | 如果无效或间隔太短
    • nextExecution

      public static ZonedDateTime nextExecution(String expression, ZonedDateTime from)
      Gets the next execution time 获取下次执行时间
      Parameters:
      expression - the cron expression | Cron表达式
      from - the start time | 开始时间
      Returns:
      the next execution time, or null | 下次执行时间,或null
    • nextExecutions

      public static List<ZonedDateTime> nextExecutions(String expression, ZonedDateTime from, int count)
      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

      public static ZonedDateTime previousExecution(String expression, ZonedDateTime from)
      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

      public static Duration getEstimatedInterval(String expression)
      Gets the estimated interval between executions 获取执行之间的预估间隔
      Parameters:
      expression - the cron expression | Cron表达式
      Returns:
      the estimated interval | 预估间隔
    • 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 | 描述
    • builder

      public static CronBuilder builder()
      Creates a new cron expression builder 创建新的Cron表达式构建器
      Returns:
      the builder | 构建器