Home > database >  Spring Cron expression every 15 mins for particular hours and not running on days
Spring Cron expression every 15 mins for particular hours and not running on days

Time:04-12

I want to run some code during fixed schedule every 15 minute every day that runs from 10 pm to 6 am and not run on 3rd and 25th of every month

Here is what I have

@Scheduled(cron = "0 0/15 0-6,22-24 * * *")
public void runJob() {
    // ...
}

0/15 - for every mins

0-6, 22-24 - assuming this runs every day from midnight to 6 am and then again from 10 pm to midnight (Is it correct)

The only thing I cant figure out is how can I say this does not run on certain dates like 3rd and 25th of every month.

Should I put something like 1,3-23,26-31 in day of month field so its something like this

0 0/15 0-6,22-24 1,4-23,26-31 * *

CodePudding user response:

You can declare analytically what you want in such a complex scenario. The following should work.

@Scheduled(cron = "0 0/15 0,1,2,3,4,5,6,22,23 1,2,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,26,27,28,29,30,31 * ? *")

Or you can summarize it a bit into

@Scheduled(cron = "0 0/15 0-6,22-23 1,2,4-24,26-31 * ? *")

For hour 24 is represented as 0

  • Related