Thursday, February 17, 2022

[SOLVED] Cron expression to run from 8PM to 6 AM

Issue

I am trying to write a cron expression for spring @Scheduled annotation that should be executed on the interval of 10 minutes between 8 PM to 6 AM -

@Scheduled(cron = "0 */10 20-06 * * MON-FRI", zone = "America/New_York")

However above cron is giving 'Invalid inverted range' exception. When I try

@Scheduled(cron = "0 */10 20-23,0-6 * * MON-FRI", zone = "America/New_York")

then it does not run between 23 and 0. Is there any way to write a cron expression that can cover 23 to 0 hours also.


Solution

use below it should run from 8 PM to 6 AM

*/10 20,0-6 * * MON-FRI    (min hour day month week)

above expression translates to -

At every 10th minute past hour 20 and every hour from 0 through 6 on every day-of-week from Monday through Friday



Answered By - Rahul Sawant
Answer Checked By - Marie Seifert (WPSolving Admin)