Hutool Hutool
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
(opens new window)
🏡首页
📖指南
🎢最佳实践
💖支持
💡javaDoc (opens new window)
⏳更新记录 (opens new window)
  • 🍎gitee (opens new window)
  • 🍏github (opens new window)
  • 快速入门

  • 核心(hutool-core)

  • 定时任务(hutool-cron)

    • 简介
    • 全局定时任务-CronUtil
    • 定时任务调度器-Scheduler
    • 全局定时任务-CronUtil
      • 介绍
      • 使用
    • 定时任务设置-CronConfig
  • 加密(hutool-crypto)

  • 数据库(hutool-db)

  • 扩展(hutool-extra)

  • HTTP(hutool-http)

  • 日志(hutool-log)

  • 配置文件(hutool-setting)

  • JSON(hutool-json)

  • Office办公(hutool-poi)

  • 套接字(hutool-socket)

  • GUI(hutool-swing)

  • 指南
  • 定时任务(hutool-cron)
Hutool
2026-01-03
目录

全局定时任务-CronUtil

# 介绍

定时任务表达式 表达式类似于Linux的crontab表达式,表达式使用空格分成5个部分,按顺序依次为:

  1. 分 :范围:0~592
  2. 时 :范围:0~23
  3. 日 :范围:1~31,"L" 表示月的最后一天
  4. 月 :范围:1~12,同时支持不区分大小写的别名:"jan","feb", "mar", "apr", "may","jun", "jul", "aug", "sep","oct", "nov", "dec"
  5. 周 :范围:0 (Sunday)~6(Saturday),7也可以表示周日,同时支持不区分大小写的别名:"sun","mon", "tue", "wed", "thu","fri", "sat","L" 表示周六

为了兼容Quartz表达式,同时支持6位和7位表达式,其中:

  1. 当为6位时,第一位表示秒 ,范围0~59,但是第一位不做匹配
  2. 当为7位时,最后一位表示年 ,范围1970~2099,但是第7位不做解析,也不做匹配

当定时任务运行到的时间匹配这些表达式后,任务被启动。 注意: 当isMatchSecond为true时才会匹配秒部分,默认都是关闭的

对于每一个子表达式,同样支持以下形式:

  • * :匹配这个位置所有的时间
  • ? :匹配这个位置任意的时间(与"*"作用一致)
  • */2 :间隔时间,例如在分上,表示每两分钟,同样*可以使用数字列表代替,逗号分隔
  • 2-8 :连续区间,例如在分上,表示2,3,4,5,6,7,8分
  • 2,3,5,8 :列表
  • cronA | cronB :多个定时表达式

注意:在每一个子表达式中优先级: 间隔(/) > 区间(-) > 列表(,)

例如 2,3,6/3中,由于/优先级高,因此相当于2,3,(6/3),结果与 2,3,6等价

一些例子:

  • 5 * * * * :每个点钟的5分执行,00:05,01:05……
  • * * * * * :每分钟执行
  • */2 * * * * :每两分钟执行
  • * 12 * * * :12点的每分钟执行
  • 59 11 * * 1,2 :每周一和周二的11:59执行
  • 3-18/5 * * * * :3~18分,每5分钟执行一次,即0:03, 0:08, 0:13, 0:18, 1:03, 1:08……

# 使用

// 每天的 12:11:39 匹配
CronPattern pattern = CronPattern.of("39 11 12 * * *");
pattern.match(DateUtil.parse("12:11:39").toCalendar(), true);
上次更新: 2026/01/03, 21:59:18
定时任务调度器-Scheduler
定时任务设置-CronConfig

← 定时任务调度器-Scheduler 定时任务设置-CronConfig→

Theme by Vdoing | Copyright © 2025-2026 Hutool | Apache-2.0
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式