Jun 7, 2018

Cron Expressions for Spring Boot


List of few important cron expressions we can use with @Scheduled annotation.

Cron expression is made up of six characters
First ==> second :) interesting ;) its unit and not the position second
Second ==> minute
Third ==> hour
Fourth ==> date
Fifth ==> month
Sixth ==> year? hmm...no its for day of week. :)

Following are some handy expressions you may want to use for your scheduled jobs.

* * * * * *      ==> Every second any Minute, any Hour, any Hour, any Date, any Month, any Day

0 * * * * *      ==> When second is zero (i.e. Every minute) any Hour, any Date, any Month, any Day

*/5 * * * * *   ==> Every five seconds any Hour, any Date, any Month, any Day (i.e. 00:00:00, 00:00:05, 00:00:10 .... 23:59:55)

0-1 * * * * *   ==> Every zeroth and first second of minute any Hour, any Date, any Month, any Day (i.e. 00:00:00, 00:00:01, 00:01:00, 00:01:01 .... 23:59:00, 23:59:01)

5,10,15 * * * * *   ==> Every fifth, tenth and fifteenth seconds of minute any Hour, any Date, any Month, any Day (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

5,10,15 * * * * THU   ==> Every fifth, tenth and fifteenth seconds of minute of every Thursday any Hour, any Date, any Month (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

5,10,15 * * * * MON,TUE,THU   ==> Every fifth, tenth and fifteenth seconds of minute of every Monday, Tuesday and Thursday any Hour, any Date, any Month (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

5,10,15 * * * * MON-FRI   ==> Every fifth, tenth and fifteenth seconds of minute of every Monday to Friday any Hour, any Date, any Month (i.e. 00:00:05, 00:00:10, 00:00:15, 00:01:05 .... 23:59:05, 23:59:10, 23:59:15)

* 40 22 * * *  ==> Every second at 22:40 (i.e. 10:40 PM) any Hour, any Date, any Month, any Day

0 40 22 * * *  ==> Only once at 22:40:00 any Day, any Month, any Day

0 30 * * * *    ==> Only once at half past of each hour any Hour, any Date, any Month, any Day (i.e. at 00:30, 01:30 .... 23:30)

0 */10 * * * * ==> Once once at every ten minutes any Hour, any Date, any Month, any Day

0 */10 0 * * * ==> Once once at every ten minutes, zeroth Hour, any Date, any Month, any Day

0 0 1 * * * ==> When month changes (welcome salary)

0 */10 0 1 * * ==> Once once at every ten minutes, zeroth Hour, first of each month, any Month, any Day

Now some fun items...

0 0 0 15 8 * ==> Every year on 15th August midnight (to welcome independence day)

0 0 0 25 12 * ==> Welcome christmas

0 0 0 1 1 * ==> Happy new year

0 30 6 * * MON-FRI ==> Wake up on working days

0 30,35 6 * * MON-FRI ==> Wake up on working days with 5 minute snooze.

0 0 6 * * SAT,SUN ==> Weekend of traveller

0 30 7 * * SAT, SUN ==> Normal weekends