Run a Spring @Scheduled task at 10 am on weekends
The Spring expression for “Weekends at 10 am” is 0 0 10 * * 0,6. Below: config you can paste, the next run times, and what to watch out for. Spring’s @Scheduled(cron) takes six fields, seconds first, and needs every set day field to match.
0 0 10 * * 0,6Open in tool“At 10:00, only on Sunday and Saturday”
@Scheduled method
@Scheduled
@Scheduled(cron = "0 0 10 * * 0,6", zone = "UTC")
public void weekendsAt10am() {
// job
}Spring gotchas
Seconds come firstSpring expressions have six fields: second, minute, hour, day of month, month, day of week. A five-field crontab line is rejected.
Both day fields must match0 0 9 1-7 * MON is the first Monday of the month. In crontab the same fields mean days 1-7 or any Monday.
Sunday is 0 or 7Unlike Quartz, where Sunday is 1. Names (MON-SUN) are safest when you move expressions between the two.
Scheduling must be switched onAdd @EnableScheduling to a configuration class or @Scheduled methods never run. By default all tasks share one thread.
FAQ
What is the Spring cron expression for “Weekends at 10 am”?
0 0 10 * * 0,6. It reads: At 10:00, only on Sunday and Saturday
Which time zone does a Spring schedule use?
In the JVM’s default time zone. Set zone = "Europe/Berlin" (any IANA name) on @Scheduled to pin it.
Can Spring cron run every 30 seconds?
Yes. Spring has a seconds field: */30 * * * * *.
Does Spring support L and #?
Yes, since Spring 5.3: L, LW, L-n and nW in day of month, and dL or d#n in day of week.