hutool.cron.cron_pattern

Cron表达式解析与匹配

Module Contents

Classes

CronPattern

Cron表达式解析与匹配

API

class hutool.cron.cron_pattern.CronPattern(pattern: str)

Cron表达式解析与匹配

支持标准5位cron: 分 时 日 月 周

特殊字符说明:

  • * : 匹配任意值

  • , : 列举多个值 (如 1,3,5)

  • - : 范围 (如 1-5)

  • / : 步长 (如 \*/5 表示每5个单位)

Initialization

初始化CronPattern

参数:

pattern – cron表达式字符串,格式为 “分 时 日 月 周”

抛出:

ValueError – 表达式格式不正确

match(dt: datetime.datetime) bool

判断给定时间是否匹配

参数:

dt – 待匹配的时间

返回:

是否匹配cron表达式

next_match_time(after: Optional[datetime.datetime] = None) datetime.datetime

获取下一次匹配时间

参数:

after – 基准时间,默认为当前时间

返回:

下一次匹配的时间

抛出:

RuntimeError – 超过搜索上限仍未找到匹配时间

static _parse_field(field: str, min_val: int, max_val: int) Set[int]

解析单个cron字段

参数:
  • field – 字段字符串

  • min_val – 最小值

  • max_val – 最大值

返回:

匹配的值集合

抛出:

ValueError – 字段格式不正确

__str__() str

返回cron表达式字符串