Cron 表達(dá)式

Cron 表達(dá)式是表示時(shí)間周期的字符串,常用于各種定時(shí)任務(wù)解決方案蠕蚜,本文中代碼示例基于 Spring @Scheduled 定時(shí)器

語(yǔ)法格式

Cron 字符串包含 6 或 7 個(gè)域麸塞,域之間使用空格分隔。

包含 6 個(gè)域的 Cron 表達(dá)式語(yǔ)法格式:

Seconds Minutes Hours DayofMonth Month DayofWeek

包含 7 個(gè)域的 Cron 表達(dá)式語(yǔ)法格式:

Seconds Minutes Hours DayofMonth Month DayofWeek Year

有效字符范圍

每個(gè)域可使用的有效字符包括:

Seconds:0~59 的整數(shù),或 , - * / 四個(gè)字符
Minutes:同 Seconds 域一致
Hours:0~23的整數(shù)兴垦,或 , - * / 四個(gè)字符
DayofMonth:0~31的整數(shù),或 , - * / ? L W C 八個(gè)字符
Month:1~12的整數(shù)字柠,或 JAN ~ DEC探越,或 , - * / 四個(gè)字符
DayofWeek:1~7的整數(shù),或 SUN ~ SAT窑业,或 , - * / ? L C # 八個(gè)字符钦幔,注意整數(shù) 1 代表星期日,每周第一天從周日開(kāi)始
Year:1970~2099常柄,或 , - * / 四個(gè)字符

特殊字符說(shuō)明

(1) ,
用于分隔枚舉值鲤氢,如在 Seconds 域使用 10,15,25 表示在第 10 秒、15 秒和 25 秒觸發(fā)一次西潘,示例代碼如下:

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

運(yùn)行日志:

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)
2018-08-01 22:42:53.326  INFO 18140 --- [           main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
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
2018-08-01 22:42:54.517  INFO 18140 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-08-01 22:42:54.538  INFO 18140 --- [           main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
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)
Task executed at 2018-08-01T22:43:10.007
Task executed at 2018-08-01T22:43:15.001
Task executed at 2018-08-01T22:43:25.001
Task executed at 2018-08-01T22:44:10.001
Task executed at 2018-08-01T22:44:15.001
Task executed at 2018-08-01T22:44:25.001
Task executed at 2018-08-01T22:45:10.001
Task executed at 2018-08-01T22:45:15.001
Task executed at 2018-08-01T22:45:25.001
......

(2) -
用于表示范圍卷玉,如在 Seconds 域使用 30-45 表示在第 30 秒至 45 秒范圍內(nèi),每秒觸發(fā)一次喷市,示例代碼如下:

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

運(yùn)行日志:

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)
2018-08-01 22:46:51.552  INFO 11236 --- [           main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
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
2018-08-01 22:46:52.524  INFO 11236 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-08-01 22:46:52.544  INFO 11236 --- [           main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
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)
Task executed at 2018-08-01T22:47:30.007
Task executed at 2018-08-01T22:47:31.002
Task executed at 2018-08-01T22:47:32.001
Task executed at 2018-08-01T22:47:33.001
Task executed at 2018-08-01T22:47:34.001
Task executed at 2018-08-01T22:47:35.001
Task executed at 2018-08-01T22:47:36.001
Task executed at 2018-08-01T22:47:37.001
Task executed at 2018-08-01T22:47:38.002
Task executed at 2018-08-01T22:47:39.001
Task executed at 2018-08-01T22:47:40.001
Task executed at 2018-08-01T22:47:41
Task executed at 2018-08-01T22:47:42.002
Task executed at 2018-08-01T22:47:43.002
Task executed at 2018-08-01T22:47:44.001
Task executed at 2018-08-01T22:47:45.002
Task executed at 2018-08-01T22:48:30.001
Task executed at 2018-08-01T22:48:31
Task executed at 2018-08-01T22:48:32.002
Task executed at 2018-08-01T22:48:33.001
Task executed at 2018-08-01T22:48:34.001
Task executed at 2018-08-01T22:48:35.001
Task executed at 2018-08-01T22:48:36.001
Task executed at 2018-08-01T22:48:37.001
Task executed at 2018-08-01T22:48:38.002
Task executed at 2018-08-01T22:48:39.001
Task executed at 2018-08-01T22:48:40.001
Task executed at 2018-08-01T22:48:41.001
Task executed at 2018-08-01T22:48:42.001
Task executed at 2018-08-01T22:48:43.002
Task executed at 2018-08-01T22:48:44
Task executed at 2018-08-01T22:48:45.001
......

(3) *
匹配所在域有效范圍內(nèi)的任意值相种,如在 Minutes 域使用則每分鐘觸發(fā)一次,示例代碼如下:

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

運(yùn)行日志:

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)
2018-08-01 22:50:18.402  INFO 12632 --- [           main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
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
2018-08-01 22:50:20.218  INFO 12632 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-08-01 22:50:20.260  INFO 12632 --- [           main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
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)
Task executed at 2018-08-01T22:51:00.007
Task executed at 2018-08-01T22:52:00.001
Task executed at 2018-08-01T22:53:00.001
......

(4) /
表示從起始時(shí)間開(kāi)始品姓,每隔固定時(shí)間觸發(fā)一次寝并,如在 Seconds 域使用 10/15 表示每分鐘內(nèi)第 10 秒觸發(fā)以后,后續(xù)每隔 15 秒觸發(fā)一次腹备,即每分鐘第 10 秒食茎、第 25 秒、第 40 秒和第 55 秒各觸發(fā)一次馏谨。注意:這種間隔循環(huán)只在每分鐘內(nèi)執(zhí)行别渔,即第 55 秒時(shí)中斷每 15 秒的間隔循環(huán),等到下一分鐘的第 10 秒再次執(zhí)行。

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

運(yùn)行日志:

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)
2018-08-01 22:56:12.085  INFO 14064 --- [           main] s.b.s.DemoSpringBootScheduledApplication : No active profile set, falling back to default profiles: default
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
2018-08-01 22:56:12.962  INFO 14064 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-08-01 22:56:12.976  INFO 14064 --- [           main] s.a.ScheduledAnnotationBeanPostProcessor : No TaskScheduler/ScheduledExecutorService bean found for scheduled processing
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)
Task executed at 2018-08-01T22:56:25.005
Task executed at 2018-08-01T22:56:40.001
Task executed at 2018-08-01T22:56:55.001
Task executed at 2018-08-01T22:57:10.001
Task executed at 2018-08-01T22:57:25.002
Task executed at 2018-08-01T22:57:40.002
Task executed at 2018-08-01T22:57:55
Task executed at 2018-08-01T22:58:10.002
......

(5) ?
只能用于 DayofMonthDayofWeek 這兩個(gè)域哎媚,表示匹配任意值喇伯,如 DayofMonth 域使用固定值 10(表示每月第 10 天觸發(fā)),則 DayofWeek 使用 ?拨与,示例代碼如下:

@Scheduled(cron = "* * * 10 * ?")
public void scheduledTask() {
    System.out.println("Task executed at " + LocalDateTime.now());
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末稻据,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子买喧,更是在濱河造成了極大的恐慌捻悯,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件淤毛,死亡現(xiàn)場(chǎng)離奇詭異今缚,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)低淡,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門姓言,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人蔗蹋,你說(shuō)我怎么就攤上這事何荚。” “怎么了猪杭?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵餐塘,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我皂吮,道長(zhǎng)戒傻,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任涮较,我火速辦了婚禮,結(jié)果婚禮上冈止,老公的妹妹穿的比我還像新娘狂票。我一直安慰自己,他們只是感情好熙暴,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布闺属。 她就那樣靜靜地躺著,像睡著了一般周霉。 火紅的嫁衣襯著肌膚如雪掂器。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,166評(píng)論 1 284
  • 那天俱箱,我揣著相機(jī)與錄音国瓮,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛乃摹,可吹牛的內(nèi)容都是我干的禁漓。 我是一名探鬼主播,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼孵睬,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼播歼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起掰读,我...
    開(kāi)封第一講書(shū)人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤秘狞,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后蹈集,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體烁试,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年雾狈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了廓潜。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡善榛,死狀恐怖辩蛋,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情移盆,我是刑警寧澤悼院,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站咒循,受9級(jí)特大地震影響据途,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜叙甸,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一颖医、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧裆蒸,春花似錦熔萧、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至辙谜,卻和暖如春俺榆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背装哆。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工罐脊, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留定嗓,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓爹殊,卻偏偏與公主長(zhǎng)得像蜕乡,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子梗夸,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容

  • 時(shí)間格式: s> m >h >d> m> w(?)> y(?) 分別對(duì)應(yīng): 秒>分>小時(shí)>日>月>周>年, 1...
    二把刀9527閱讀 2,332評(píng)論 0 4
  • Cron表達(dá)式是一個(gè)字符串层玲,字符串以5或6個(gè)空格隔開(kāi),分為6或7個(gè)域反症,每一個(gè)域代表一個(gè)含義辛块,Cron有如下兩種語(yǔ)法...
    江公子_閱讀 6,692評(píng)論 1 1
  • 1.Cron是什么,用來(lái)做什么的 根據(jù)百度百科的解釋:計(jì)劃任務(wù)铅碍,是任務(wù)在約定的時(shí)間執(zhí)行已經(jīng)計(jì)劃好的工作润绵,這是表面的...
    有點(diǎn)胖的小樂(lè)樂(lè)閱讀 867評(píng)論 0 0
  • Cron表達(dá)式是一個(gè)字符串,字符串以5或6個(gè)空格隔開(kāi)胞谈,分為6或7個(gè)域尘盼,每一個(gè)域代表一個(gè)含義,Cron有如下兩種語(yǔ)法...
    sakura_L閱讀 315評(píng)論 0 0
  • 記錄一下今天體檢被說(shuō)有問(wèn)題的: 咽炎烦绳、外痔卿捎; 甲狀腺結(jié)節(jié); 乳腺增生&左乳結(jié)節(jié)(建議3個(gè)月到半年檢查一次)径密; 輕度...
    適說(shuō)心語(yǔ)閱讀 168評(píng)論 0 0