Cron 表达式是表示时间周期的字符串,常用于各种定时任务解决方案,本文中代码示例基于 Spring @Scheduled 定时器

语法格式

Cron 字符串包含 6 或 7 个域,域之间使用空格分隔。

包含 6 个域的 Cron 表达式语法格式:

Seconds Minutes Hours DayofMonth Month DayofWeek

包含 7 个域的 Cron 表达式语法格式:

Seconds Minutes Hours DayofMonth Month DayofWeek Year

有效字符范围

每个域可使用的有效字符包括:

Seconds:0~59 的整数,或 , - * / 四个字符
Minutes:同 Seconds 域一致
Hours:0~23的整数,或 , - * / 四个字符
DayofMonth:0~31的整数,或 , - * / ? L W C 八个字符
Month:1~12的整数,或 JAN ~ DEC,或 , - * / 四个字符
DayofWeek:1~7的整数,或 SUN ~ SAT,或 , - * / ? L C # 八个字符,注意整数 1 代表星期日,每周第一天从周日开始
Year:1970~2099,或 , - * / 四个字符

特殊字符说明

(1) ,
用于分隔枚举值,如在 Seconds 域使用 10,15,25 表示在第 10 秒、15 秒和 25 秒触发一次,示例代码如下:

  1. @Scheduled(cron = "10,15,25 * * * * ?")
  2. public void scheduledTask() {
  3. System.out.println("Task executed at " + LocalDateTime.now());
  4. }

运行日志:

  1. 2018-08-01 22:42:53.322 INFO 18140 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 18140 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
  2. 2018-08-01 22:42:53.326 INFO 18140 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
  3. 2018-08-01 22:42:53.382 INFO 18140 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@77e4c80f: startup date [Wed Aug 01 22:42:53 CST 2018]; root of context hierarchy
  4. 2018-08-01 22:42:54.517 INFO 18140 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  5. 2018-08-01 22:42:54.538 INFO 18140 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
  6. 2018-08-01 22:42:54.563 INFO 18140 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 1.521 seconds (JVM running for 1.976)
  7. Task executed at 2018-08-01T22:43:10.007
  8. Task executed at 2018-08-01T22:43:15.001
  9. Task executed at 2018-08-01T22:43:25.001
  10. Task executed at 2018-08-01T22:44:10.001
  11. Task executed at 2018-08-01T22:44:15.001
  12. Task executed at 2018-08-01T22:44:25.001
  13. Task executed at 2018-08-01T22:45:10.001
  14. Task executed at 2018-08-01T22:45:15.001
  15. Task executed at 2018-08-01T22:45:25.001
  16. ......

(2) -
用于表示范围,如在 Seconds 域使用 30-45 表示在第 30 秒至 45 秒范围内,每秒触发一次,示例代码如下:

  1. @Scheduled(cron = "30-45 * * * * ?")
  2. public void scheduledTask() {
  3. System.out.println("Task executed at " + LocalDateTime.now());
  4. }

运行日志:

  1. 2018-08-01 22:46:51.549 INFO 11236 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 11236 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
  2. 2018-08-01 22:46:51.552 INFO 11236 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
  3. 2018-08-01 22:46:51.600 INFO 11236 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@b7f23d9: startup date [Wed Aug 01 22:46:51 CST 2018]; root of context hierarchy
  4. 2018-08-01 22:46:52.524 INFO 11236 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  5. 2018-08-01 22:46:52.544 INFO 11236 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
  6. 2018-08-01 22:46:52.566 INFO 11236 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 1.29 seconds (JVM running for 1.72)
  7. Task executed at 2018-08-01T22:47:30.007
  8. Task executed at 2018-08-01T22:47:31.002
  9. Task executed at 2018-08-01T22:47:32.001
  10. Task executed at 2018-08-01T22:47:33.001
  11. Task executed at 2018-08-01T22:47:34.001
  12. Task executed at 2018-08-01T22:47:35.001
  13. Task executed at 2018-08-01T22:47:36.001
  14. Task executed at 2018-08-01T22:47:37.001
  15. Task executed at 2018-08-01T22:47:38.002
  16. Task executed at 2018-08-01T22:47:39.001
  17. Task executed at 2018-08-01T22:47:40.001
  18. Task executed at 2018-08-01T22:47:41
  19. Task executed at 2018-08-01T22:47:42.002
  20. Task executed at 2018-08-01T22:47:43.002
  21. Task executed at 2018-08-01T22:47:44.001
  22. Task executed at 2018-08-01T22:47:45.002
  23. Task executed at 2018-08-01T22:48:30.001
  24. Task executed at 2018-08-01T22:48:31
  25. Task executed at 2018-08-01T22:48:32.002
  26. Task executed at 2018-08-01T22:48:33.001
  27. Task executed at 2018-08-01T22:48:34.001
  28. Task executed at 2018-08-01T22:48:35.001
  29. Task executed at 2018-08-01T22:48:36.001
  30. Task executed at 2018-08-01T22:48:37.001
  31. Task executed at 2018-08-01T22:48:38.002
  32. Task executed at 2018-08-01T22:48:39.001
  33. Task executed at 2018-08-01T22:48:40.001
  34. Task executed at 2018-08-01T22:48:41.001
  35. Task executed at 2018-08-01T22:48:42.001
  36. Task executed at 2018-08-01T22:48:43.002
  37. Task executed at 2018-08-01T22:48:44
  38. Task executed at 2018-08-01T22:48:45.001
  39. ......

(3) *
匹配所在域有效范围内的任意值,如在 Minutes 域使用则每分钟触发一次,示例代码如下:

  1. @Scheduled(cron = "0 * * * * ?")
  2. public void scheduledTask() {
  3. System.out.println("Task executed at " + LocalDateTime.now());
  4. }

运行日志:

  1. 2018-08-01 22:50:18.396 INFO 12632 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 12632 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
  2. 2018-08-01 22:50:18.402 INFO 12632 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
  3. 2018-08-01 22:50:18.491 INFO 12632 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@709ba3fb: startup date [Wed Aug 01 22:50:18 CST 2018]; root of context hierarchy
  4. 2018-08-01 22:50:20.218 INFO 12632 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  5. 2018-08-01 22:50:20.260 INFO 12632 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
  6. 2018-08-01 22:50:20.288 INFO 12632 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 2.406 seconds (JVM running for 3.198)
  7. Task executed at 2018-08-01T22:51:00.007
  8. Task executed at 2018-08-01T22:52:00.001
  9. Task executed at 2018-08-01T22:53:00.001
  10. ......

(4) /
表示从起始时间开始,每隔固定时间触发一次,如在 Seconds 域使用 10/15 表示每分钟内第 10 秒触发以后,后续每隔 15 秒触发一次,即每分钟第 10 秒、第 25 秒、第 40 秒和第 55 秒各触发一次。注意:这种间隔循环只在每分钟内执行,即第 55 秒时中断每 15 秒的间隔循环,等到下一分钟的第 10 秒再次执行。

  1. @Scheduled(cron = "10/15 * * * * ?")
  2. public void scheduledTask() {
  3. System.out.println("Task executed at " + LocalDateTime.now());
  4. }

运行日志:

  1. 2018-08-01 22:56:12.082 INFO 14064 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Starting DemoSpringBootScheduledApplication on LAPTOP-C375ASPB with PID 14064 (D:\JYL\DEV\IdeaProjects\demo\demo-spring-boot-scheduled\target\classes started by Ji in D:\JYL\DEV\IdeaProjects\demo)
  2. 2018-08-01 22:56:12.085 INFO 14064 --- [ main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
  3. 2018-08-01 22:56:12.131 INFO 14064 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1dd92fe2: startup date [Wed Aug 01 22:56:12 CST 2018]; root of context hierarchy
  4. 2018-08-01 22:56:12.962 INFO 14064 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
  5. 2018-08-01 22:56:12.976 INFO 14064 --- [ main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
  6. 2018-08-01 22:56:12.988 INFO 14064 --- [ main] s.b.s.DemoSpringBootScheduledApplication : Started DemoSpringBootScheduledApplication in 1.191 seconds (JVM running for 1.68)
  7. Task executed at 2018-08-01T22:56:25.005
  8. Task executed at 2018-08-01T22:56:40.001
  9. Task executed at 2018-08-01T22:56:55.001
  10. Task executed at 2018-08-01T22:57:10.001
  11. Task executed at 2018-08-01T22:57:25.002
  12. Task executed at 2018-08-01T22:57:40.002
  13. Task executed at 2018-08-01T22:57:55
  14. Task executed at 2018-08-01T22:58:10.002
  15. ......

(5) ?
只能用于 DayofMonthDayofWeek 这两个域,表示匹配任意值,如 DayofMonth 域使用固定值 10(表示每月第 10 天触发),则 DayofWeek 使用 ?,示例代码如下:

  1. @Scheduled(cron = "* * * 10 * ?")
  2. public void scheduledTask() {
  3. System.out.println("Task executed at " + LocalDateTime.now());
  4. }