Spring cron format
A Spring expression has six fields: second, minute, hour, day of month, month and day of week, for example 0 0 9 * * MON-FRI for 09:00 on weekdays. Sunday can be written as 0, 7 or SUN. Since Spring Framework 5.3 the parser also understands L, W and # in the day fields, plus macros such as @hourly and @daily.
Time zone of @Scheduled
By default expressions are evaluated in the server’s default time zone. Set the zone attribute, for example @Scheduled(cron = "0 0 2 * * *", zone = "Europe/Paris"), to make the schedule independent of the host. Choose the same zone in the tool to preview the runs.
Spring vs Quartz
Spring expressions look like Quartz ones but differ in details: there is no year field, Sunday is 0 rather than 1, and “?” is accepted but not required. When both day fields are restricted Spring requires both to match. The converter shows the Quartz equivalent when it exists.
Enabling scheduling
Cron methods only run when scheduling is switched on with @EnableScheduling. By default Spring uses a single thread, so a long task delays every other scheduled method; configure a ThreadPoolTaskScheduler or spring.task.scheduling.pool.size when you have several jobs. In a cluster every instance fires the same cron, so use a lock such as ShedLock if a job must run only once.
Frequently asked questions
- Why does Spring say my cron expression must have six fields?
- Spring needs a seconds field. Prepend 0 to a five-field crontab expression: 0 */5 * * * * runs every five minutes.
- How do I disable a @Scheduled cron job?
- Use the special value "-" (Scheduled.CRON_DISABLED), usually through a property placeholder.
- Does Spring support the last day of the month?
- Yes, since 5.3: 0 0 0 L * * runs at midnight on the last day of every month.
- Can I read the cron expression from configuration?
- Yes, for example @Scheduled(cron = "${jobs.cleanup.cron}"), so the schedule can change per environment.