Search code examples
cronquartz-schedulercronexpression

Overnight Cron Expression


I want to run a scheduler that runs at the following hours:

Monday 2100 to Tuesday 0800
Tuesday 2100 to Wednesday 0800
Wednesday 2100 to Thursday 0800
Thursday 2100 to Friday 0800
Friday 2100 to Saturday 0800 

I have this cron expression which seems to cover all cases:

* 0 0-8,21-23 ? * MON-SAT

The obvious issue with overnight cron expressions is that as they run over into the next day you are going to get overlap. For example the above cron expression will run Monday 0000 to 0800 and Saturday 0900 to 2300 which I don't want it to. anyway to fix this?


Solution

  • In case anyone was interested the only way I could find to solve this was to use two Cron triggers, was the only way you can avoid the overflow.

    0 0 0-8 ? * TUE-SAT
    0 0 21-23 ? * MON-FRI