Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 * *

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading