cron expression explanation

(cron = "* * * * * *")

cron expression format

{Seconds} {minute} {hour} {date} {month} {week} {particular year(Can be empty)}

For example, "0 0 12? * wed" is executed at 12:00 p.m. every Wednesday (the year is usually omitted)
First understand the meaning of each position, the allowable range of each position, some special writing methods, and common cases, which is enough for you to master cron expressions

1: Allowable values for each field

fieldAllowable valueAllowed special characters
second0-59- * /
branch0-59- * /
hour0-23- * /
date1-31- * ? / L W C
month1-12 or JAN-DEC- * /
week1-7 or SUN-SAT- * ? / L C #
Year (optional)1970-2099- * /

2: Allowable value means:

Seconds: it can be represented by numbers 0-59,
Minutes: it can be represented by the number 0-59,
Hours: it can be represented by numbers 0-23,
Day of month: you can use any one of the numbers 1-31, but pay attention to some special months
Month: it can be expressed by 0-11 or the string "JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV and DEC"
Day of week: it can be represented by numbers 1-7 (1 = Sunday) or by character string "SUN, MON, TUE, WED, THU, FRI and SAT"

3: Meaning of each symbol:

*Represents all values;
? Indicates an unspecified value, that is, it doesn't care what it is;
-Represents a specified range;
, indicating an additional possible value;
/Before the symbol indicates the start time, and after the symbol indicates the value of each increment;
L("last") ("last") "L" is used in the day of month field, meaning "the last day of the month"; Used in the day of week field, it simply means "7" or "SAT". If it is used in combination with numbers in the day of week field, it means "the last day of the month" – for example, "6L" means "the last Friday of the month". When we use "L", it is important not to specify a list value or range, otherwise we will get some unexpected results.
W("weekday") can only be used in the day of month field. Used to describe the working day closest to the specified day (Monday to Friday). For example, in the day of month field, "15W" refers to "the working day closest to the 15th day of this month", that is, if the 15th day of this month is Saturday, the trigger will be triggered on the 14th day of this month, that is, Friday; If the 15th day of the month is Sunday, the trigger will be triggered on the 16th day of the month, that is, Monday; If the 15th day of the month is Tuesday, it will be triggered on the trigger day. Note: this usage will only calculate the value in the current month and will not exceed the current month. The "W" character can only indicate a day in the day of month and cannot be a range or list. You can also use "LW" to specify the last working day of the month.
#Can only be used in the day of week field. Used to specify the week ordinal of the month. Example: in the day of week field, "6#3" refers to the third Friday of the month (6 refers to Friday and 3 refers to the third). If the specified date does not exist, the trigger will not be triggered.
C refers to the value calculated after contacting calendar. For example, "5C" in the day of month field refers to the first day of the calendar on or after the fifth day of the month; In the day of week field, "1C" refers to the first day including the calendar on or after this Sunday.

4: Some cron expression cases

"*/5 * * * * ?" Every 5 seconds

"0 */1 * * * ?" Every 1 minute
 
"0 0 5-15 * * ?"  5 per day-15 Point on point trigger
 
"0 0/3 * * * ?" Triggered every three minutes
 
"0 0-5 14 * * ?" Every day from 2 p.m. to 2 p.m:05 Triggered every 1 minute during 
 
"0 0/5 14 * * ?" Every day from 2 p.m. to 2 p.m:55 Triggered every 5 minutes during
 
"0 0/5 14,18 * * ?" Every day from 2 p.m. to 2 p.m:55 During and from 6 p.m. to 6 p.m:55 Triggered every 5 minutes during
 
"0 0/30 9-17 * * ?" Every half an hour during nine to five working hours
 
" 0 0 10,14,16 * * ?" Every day at 10 a.m., 2 p.m. and 4 p.m 
 
"0 0 12 ? * WED" It means 12 noon every Wednesday
 
"0 0 17 ? * TUES,THUR,SAT" Every Tuesday, Thursday and Saturday at 5 p.m
 
"0 10,44 14 ? 3 WED" Every Wednesday in March at 2 p.m:10 And 2:44 trigger 
 
"0 15 10 ? * MON-FRI" Monday to Friday at 10 a.m:15 trigger
 
"0 0 23 L * ?" At 23:00 on the last day of each month
 
"0 15 10 L * ?" 10 a.m. on the last day of each month:15 trigger 
 
"0 15 10 ? * 6L" 10 a.m. on the last Friday of each month:15 trigger 
 
"0 15 10 * * ? 2005 2005" 10 a.m. every day in:15 trigger 
 
"0 15 10 ? * 6L 2002-2005" 2002 10 a.m. on the last Friday of each month from 2005 to 2005:15 trigger 
 
"0 15 10 ? * 6#3 "triggered at 10:15 a.m. on the third Friday of each month

"30 * * * * ?" Trigger task every half minute

"30 10 * * * ?" Trigger the task every 10 minutes and 30 seconds of the hour

"30 10 1 * * ?" Trigger the task at 1:10:30 every day

"30 10 1 20 * ?" The task is triggered at 1:10:30 on the 20th of each month

"30 10 1 20 10 ? *" The task is triggered at 1:10:30 on October 20 every year

"30 10 1 20 10 ? 2011" 2011 The task was triggered at 1:10:30 on October 20, 2013

"30 10 1 ? 10 * 2011" 2011 In October, the task was triggered at 1:10:30 every day

"30 10 1 ? 10 SUN 2011" 2011 The task is triggered at 1:10:30 every Sunday in October

"15,30,45 * * * * ?" Trigger the task every 15 seconds, 30 seconds and 45 seconds

"15-45 * * * * ?" 15 Within 45 seconds, the task is triggered every second

"15/5 * * * * ?" Trigger every 15 seconds of every minute and every 5 seconds

"15-30/5 * * * * ?" The trigger starts from 15 seconds to 30 seconds per minute and is triggered every 5 seconds

"0 0/3 * * * ?" Starting from the 0 minute 0 second of each hour, it is triggered every three minutes

"0 15 10 ? * MON-FRI" The task is triggered at 10:15:0 from Monday to Friday

"0 15 10 L * ?" The task is triggered at 10:15:00 on the last day of each month

"0 15 10 LW * ?" The task is triggered at 10:15:00 on the last working day of each month

"0 15 10 ? * 5L" The task is triggered at 10:15:00 on the last Thursday of each month

"0 15 10 ? * 5#3 "the task is triggered at 10:15:00 on Thursday of the third week of each month

5: Expression builder

Recommend several cron expression online generators

http://www.pdtools.net/tools/becron.jsp

https://tool.ityuan.com/cron

http://www.matools.com/cron

http://cron.qqe2.com/

Tags: Java Spring Boot

Posted on Sun, 19 Sep 2021 11:34:23 -0400 by examancer