Search code examples
javacronschedulecrontrigger

How to schedule crontrigger to run on certain days of the week?


I have a job to execute on Saturday and Sunday, but not on weekdays. This is how I schedule it:

myTriggerBuilder.withSchedule(cronSchedule("0 0 0 * * ?")).build();

The scheduler in use is Quartz.

This will run at 00:00:00 server time each day. However, I would like to make it work only on Saturday and Sunday, however, in American calendars Saturday is the end of the week and Sunday is the start of the week. I have been searching the docs for an example or description which explains how can I specify certain days of the week rather than intervals, but the docs either does not provide that information, or I have missed it. I have tried it this way:

myTriggerBuilder.withSchedule(cronSchedule("0 0 0 * * SAT,SUN")).build();

However, the whole thing crashed:

java.lang.RuntimeException: CronExpression '0 0 0 * * SAT,SUN' is invalid.

Is there a way to express what I want, that is, to tell the scheduler which days of the week I intend to run the job?


Solution

  • Can you please try this?

    0 0 0 ? * SUN,SAT *
    

    Refer: This