Search code examples
javacronquartz-schedulercrontrigger

Cron expression for the second to last day of week of the month


I want to trigger the second to last day of week of the month.

Typically, here is the last friday of the month (ex : 30/06/2017)

0 0 0 ? * FRIL 

And I want the second to last (ex : 23/06/2017)

0 0 0 ? * FRIL-1 

But this syntax return the same result as before (with the Quartz scheduler and with cronmaker)

The second to last day of week of the month can appear either the 3rd or the 4th week of the month. So it's :

either : 0 0 0 ? * FRI#3 
or     : 0 0 0 ? * FRI#4 

Do you have any advice ?


Solution

  • I don't think you can express that using cron syntax.
    I can think of some work-arounds, though:

    • You could schedule your job every Friday and have some in-job logic to check whether it is actually the second-to-last before going on.

    • Another option would be to create a dummy cron trigger for the last Friday, retrieve the "next fire time", substract 7 days from that date and create the actual trigger using that exact date -- but you'd have to do that for every week (either by pre-scheduling several triggers, or by having your job re-schedule itself after each run).