Class CronExpression

java.lang.Object
cloud.opencode.base.date.cron.CronExpression
All Implemented Interfaces:
Serializable

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

  • Method Details

    • parse

      public static CronExpression parse(String expression)
      Parses a cron expression 解析cron表达式
      Parameters:
      expression - the cron expression | cron表达式
      Returns:
      the CronExpression | CronExpression
      Throws:
      OpenDateException - if the expression is invalid | 如果表达式无效则抛出异常
    • daily

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

      public static CronExpression everyMinutes(int minutes)
      Creates a cron expression for every N minutes 为每N分钟创建cron表达式
      Parameters:
      minutes - the interval in minutes | 分钟间隔
      Returns:
      the CronExpression | CronExpression
    • everyHours

      public static CronExpression everyHours(int hours)
      Creates a cron expression for every N hours 为每N小时创建cron表达式
      Parameters:
      hours - the interval in hours | 小时间隔
      Returns:
      the CronExpression | CronExpression
    • nextExecution

      public LocalDateTime nextExecution()
      Gets the next execution time after now 获取当前之后的下一次执行时间
      Returns:
      the next execution time | 下一次执行时间
    • nextExecution

      public LocalDateTime nextExecution(LocalDateTime after)
      Gets the next execution time after the specified time 获取指定时间之后的下一次执行时间
      Parameters:
      after - the starting time | 开始时间
      Returns:
      the next execution time | 下一次执行时间
    • previousExecution

      public LocalDateTime previousExecution()
      Gets the previous execution time before now 获取当前之前的上一次执行时间
      Returns:
      the previous execution time | 上一次执行时间
    • previousExecution

      public LocalDateTime previousExecution(LocalDateTime before)
      Gets the previous execution time before the specified time 获取指定时间之前的上一次执行时间
      Parameters:
      before - the ending time | 结束时间
      Returns:
      the previous execution time | 上一次执行时间
    • nextExecutions

      public List<LocalDateTime> nextExecutions(int count)
      Gets the next N execution times 获取接下来的N次执行时间
      Parameters:
      count - the number of executions | 执行次数
      Returns:
      the list of execution times | 执行时间列表
    • nextExecutions

      public List<LocalDateTime> nextExecutions(LocalDateTime after, int count)
      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

      public boolean matches(LocalDateTime dateTime)
      Checks if the expression matches the specified date-time 检查表达式是否匹配指定的日期时间
      Parameters:
      dateTime - the date-time to check | 要检查的日期时间
      Returns:
      true if matches | 如果匹配返回true
    • getExpression

      public String getExpression()
      Gets the original expression string 获取原始表达式字符串
      Returns:
      the expression | 表达式
    • getMinutes

      public Set<Integer> getMinutes()
      Gets the allowed minutes 获取允许的分钟
      Returns:
      the minutes | 分钟集合
    • getHours

      public Set<Integer> getHours()
      Gets the allowed hours 获取允许的小时
      Returns:
      the hours | 小时集合
    • getDaysOfMonth

      public Set<Integer> getDaysOfMonth()
      Gets the allowed days of month 获取允许的日期
      Returns:
      the days of month | 日期集合
    • getMonths

      public Set<Integer> getMonths()
      Gets the allowed months 获取允许的月份
      Returns:
      the months | 月份集合
    • getDaysOfWeek

      public Set<Integer> getDaysOfWeek()
      Gets the allowed days of week 获取允许的星期几
      Returns:
      the days of week | 星期几集合
    • describe

      public String describe()
      Gets a human-readable description of the cron expression 获取cron表达式的人类可读描述
      Returns:
      the description | 描述
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object