Timer cron expression

                                                   Cron expression

 

I. Introduction

       In development, we often need to use timed tasks (quartz) to achieve business requirements;

           Often the headache is not the logic, but the final, how to define the cron expression (not difficult, but easy to forget ^_^).

           The summary is as follows:

Two, Cron expression definition

       ①, listed  a few cron expression Example:       

           0 0 0 * *? Meaning: execute a timed task at 0:00 every day

           0 0 0? * MON Meaning: execute a timed task every Monday at 0:00

           0 0 0 1 *? Meaning: execute a scheduled task at 0:00 on the 1st of every month

      ②, this  kind of format can be determined:

                [Second, Minute, Day, Month, Anniversary], where the year is an optional type, which means it is every year without setting the year and minute.

                             ↓↓↓↓↓↓↓↓

      ③, format  defined by the formula      

Field

allowance

Special characters allowed

Seconds  (seconds)

0-59

, - * /

Minutes(分)

0-59

, - * /

Hours (time)

0-23

, - * /

Day-of-Month(天)

1-31

, - * ? / L W C

Month (month)

1-12 or JAN-DEC ( a )

, - * /

Day-of-Week(周)

1-7 or SUN-SAT ( b )

, - * ? / L C #

Year (optional field)

Leave blank, 1970-2099

, - * /

 

                (a). JAN,FEB,MAR,APR,MAY,JUNE,JULY,AUG,SEP,OCT,NOW,DEC

                ( b ). SUN , MON , TUE , WED , THU , FRI , SAT

     ③, special  special character definition

               "*" character: wildcard, specify all values;

                               eg: 0 * * * *? means to execute once every minute

                               eg: 0 * *? * * means to execute once every 00 seconds

 

               "?" Character: Only used in the date field and week of the week field, specify "non-specific value";

                               eg: 0 10 18 15 3? means that every year on the 15th day of March, it will be executed once at 6:10 pm

                               eg: 0 10 18? 3 WEB means that every Wednesday in March every year, it will be executed once at 6:10 pm

 

               "," character: Indicates to list enumerated values;

                               eg: 10,20 * *? * * means that it will be executed once every 10th and 20th seconds

                               eg: 0 10,20 * 1,2 *? Represents the first and second days of each month, and the first day of each hour

                                                                 Execute every 10 minutes and 20 minutes

 

               "-" Character: Specify a range;

                               eg: 0 10 18 1-5 *? means the 1st to the 5th of each month (including the 1/5th, a total of 5 days per month),

                                                              Executed at 6:10 PM

                               eg: 0 10-15 *? * * means the 10th minute to the 15th minute of each hour (including 10/15 minutes,

                                                               (A total of 5 minutes per hour) are executed once

 

               "/" Character: Specify the increment of a value. n/m means starting from n and increasing by m each time;

                               eg: 5/20 * *? * * means that it will be executed every 5th, 25th and 45th seconds every minute

                               eg: 0 * 2/2? * * means that every day 2/4/6/8.../22 hours 0 minutes 0 seconds are executed once

 

               "L" character: only used in the date field and day of the week field, used in the day to represent the last day of the month,

                                Used in weeks to indicate the last week of the month X;

 

               "W" character: only appear in the date field, specify the working day closest to the given date (Monday to Friday),

                                "L" and "W" can be used jointly in the date field, LW represents the working day of the last week of the month;

 

               "#" character: only appears in the week field, indicating the week X of the month. 6#3 means the third Friday of the month;

                               eg: * * *? * 3#4 means that it will be executed once in the early morning on week 2 of the 4th week of each month

                               eg: * * *? * 6#2 means that every 5th week of the second week of each month, it will be executed once in the early morning

 

               "C" character: only appears in the date field and week field, this character depends on a designated "calendar".

 

Three, common cron expression examples

 

  •                  */5 * * * *?:   Execute once every 5 seconds;
  •                  0 */1 * * *?:   Execute once every 1 minute;
  •                  0 0 23 * *?:   Execute once every day at 23:00;
  •                  0 0 1 * *?:   Execute once every day at 1:00 am;
  •                  0 0 1 1 *?:   Execute once every month at 1:00 AM on the 1st;
  •                  0 0 23 L *?:   Execute once at 23:00 on the last day of each month;
  •                  0 0 1? * L   : Execute once a week at 1:00 AM on Sundays;
  •                  0 0 2 1 *? *   : Execute at 2 am on the 1st of each month;
  •                  0 0 12 * *?   :Execute at 12 noon every day;
  •                  0 15 10? * *   : execute at 10:15 every morning;
  •                  0 15 10 * *?:   Executed at 10:15 am every day in 2005;
  •                  0 * 14 * *?:   Execute every 1 minute from 2 pm to 2:59 pm every day;
  •                  0 0/5 14 * *?:   Execute every 5 minutes from 2 pm to 2:55 pm every day;
  •                  0 26,29,33 * * *?:   Perform once at 26, 29, and 33 points;
  •                  0 0 0,13,18,21 * *?: It will   be executed once every day at 0 o'clock, 13 o'clock, 18 o'clock, and 21 o'clock;
  •                  0 15 10? * MON-FRI   : means that it will be executed at 10:15 am every day from Monday to Friday;
  •                  0 15 10? 6L 2002-2006   : means that it will be executed at 10:15 am on the last Friday of each month from 2002 to 2006;
  •                  0 0 10,14,16 * *?: Execute   at 10 am and 2 pm/4 pm every day;
  •                  0 0/30 9-17 * *?:   Execute every half hour during the working hours from 9 to 5;
  •                  0 0 12? * WED   : means that it will be executed every Wednesday at 12 noon;
  •                  0 0/5 14,18 * *?:   Execute every 5 minutes during the period from 2 pm to 2:55 pm and from 6 pm to 6:55 pm;
  •                  0 0-5 14 * *?:   Execute every 1 minute from 2 pm to 2:05 pm every day;
  •                  0 10,44 14? ​​3 WED   : execute at 2:10 and 2:44 pm on Wednesday in March every year;
  •                  0 15 10? * MON-FRI   : execute at 10:15 am from Monday to Friday;
  •                  0 15 10 15 * ?   : executed at 10:15 am on the 15th of each month;
  •                  0 15 10 L * ?   : executed at 10:15 am on the last day of each month;
  •                  0 15 10? * 6L   : executed at 10:15 am on the last Friday of each month;
  •                  0 15 10? * 6L 2002-2005   : executed at 10:15 am on the last Friday of each month from 2002 to 2005;
  •                  0 15 10? * 6#3   : execute at 10:15 am on the third Friday of each month;

Four, friendship link

       In the  auto-generated URL line Cron expression:

           http://cron.qqe2.com/

Guess you like

Origin blog.csdn.net/qq_30960647/article/details/109308043