Home › Spring Boot @Scheduled Generator
Spring Boot @Scheduled Cron Generator
Spring cron uses 6 fields: second minute hour day month weekday. The day-of-week and day-of-month conflict is handled with ? - if you set a specific weekday, day must be * or ?.
Quick presets
0 * * * * ?
Runs every minute
Use in Spring Boot @Scheduled
@Scheduled annotation:
@Component
public class MyScheduledTask {
@Scheduled(cron = "0 * * * * ?")
public void runTask() {
// task logic here
}
}
Using a property (recommended for production):
@Scheduled(cron = "${my.task.cron}")
public void runTask() { ... }
In application.properties:
my.task.cron=0 * * * * ?
@Scheduled Cron vs Fixed Rate
@Scheduled accepts three different attributes - they are not interchangeable. Picking the wrong one is the most common Spring scheduling mistake.
| Attribute | Meaning | Example |
|---|---|---|
cron | Calendar-based schedule, like crontab | cron = "0 0 9 * * MON-FRI" |
fixedRate | Run every N ms, measured from start to start | fixedRate = 5000 |
fixedDelay | Run every N ms, measured from end to next start | fixedDelay = 5000 |
Use cron whenever you need a specific time of day or day of week - the other two only express simple repeating intervals from app startup, not wall-clock time.
Spring vs Linux Cron
| Feature | Linux | Spring Boot |
|---|---|---|
| Fields | 5 | 6 |
| Field order | min hr dom month dow | sec min hr dom month dow |
| Seconds | No | Yes (1st field) |
? operator | No | Yes |
L operator | No | Yes |
| Timezone | Server TZ | App TZ (configurable) |
@Scheduled Setup Tips
Enable scheduling first
@Scheduled does nothing on its own. Add @EnableScheduling to your main class or a configuration class, or the annotation is silently ignored:
@SpringBootApplication
@EnableScheduling
public class MyApp { ... }
Set timezone per task
Available from Spring 5.3+:
@Scheduled(cron = "0 0 9 * * MON-FRI",
zone = "Europe/Madrid")
public void morningTask() { ... }
Use properties, not hardcoded values
Hardcoding cron expressions in annotations makes them impossible to change without redeployment.
Use ${property.name} so you can change the schedule via application.properties or environment variables.
See the Spring @Scheduled Javadoc.
# application.properties app.cron.digest=0 0 9 * * MON-FRI
# application.yml
app:
cron:
digest: "0 0 9 * * MON-FRI"
@Scheduled(cron = "${app.cron.digest}", zone = "Europe/Madrid")
public void sendDigest() { ... }
Disable a scheduled task via properties
Set the cron value to - to disable a scheduled task without removing the annotation. Useful for disabling a job in specific environments:
# application-dev.properties app.cron.digest=- # disabled in dev
@Scheduled(cron = "${app.cron.digest:-}")
public void sendDigest() { ... }
The :- syntax sets - as the default value if the property is not defined.
@Scheduled methods must return void
A scheduled method's return value is always ignored. Methods must also take no arguments - Spring has nothing to pass in.
Spring Batch scheduled jobs
For Spring Batch, trigger a JobLauncher from a @Scheduled method.
Same cron syntax applies - 6 fields, seconds first.
Spring Cron Common Errors
Cron expression must consist of 6 fields
This is the most common Spring cron error. Spring requires 6 fields - a 5-field Linux cron expression will fail:
// Linux cron (5 fields) - FAILS in Spring: @Scheduled(cron = "0 9 * * MON-FRI") // Error: Cron expression must consist of 6 fields // Spring cron (6 fields) - correct: @Scheduled(cron = "0 0 9 * * MON-FRI")
Add the seconds field (usually 0) at the beginning.
The ? sign in Spring cron
When you specify a weekday, the day-of-month field should be ? (not *), and vice versa:
// Weekday specified → DOM should be ? @Scheduled(cron = "0 0 9 ? * MON-FRI") // DOM specified → DOW should be ? @Scheduled(cron = "0 0 0 15 * ?") // Both * also works in Spring (unlike strict Quartz) @Scheduled(cron = "0 0 9 * * MON-FRI")
Spring's parser is slightly more lenient than Java Quartz on this rule, but using ? is the recommended practice.
@Scheduled not firing at all
If a @Scheduled method never runs, the most likely cause is missing @EnableScheduling. Add it to your main class or a @Configuration class. Also check that the bean is managed by Spring - scheduling does not work on objects created with new.
Year field not supported
Spring's cron parser does not support the optional 7th Year field that Java Quartz accepts. Expressions with 7 fields will be rejected. Use exactly 6 fields.
Spring Boot Cron Expression Examples
| Expression | Description | |
|---|---|---|
0 * * * * ? | Every minute at second 0 | |
0 */5 * * * ? | Every 5 minutes | |
0 */15 * * * ? | Every 15 minutes | |
*/10 * * * * ? | Every 10 seconds | |
0 0 * * * ? | Every hour on the hour | |
0 0 5/8 * * ? | Every 8 hours starting at 5 AM (5, 13, 21) | |
0 0 */6 * * ? | Every 6 hours (0, 6, 12, 18) | |
0 0 0 * * ? | Every day at midnight | |
0 0 7 * * ? | Every day at 7:00 AM | |
0 30 13 * * ? | Every day at 1:30 PM | |
0 0 9 * * MON-FRI | Weekdays at 9:00 AM | |
0 45 13 * * MON-SAT | Mon-Sat at 1:45 PM | |
0 0 0 1 * ? | First of every month at midnight | |
0 0 0 L * ? | Last day of every month | |
0 30 8 * * MON | Every Monday at 8:30 AM | |
0 0 6,18 * * ? | Twice daily at 6 AM and 6 PM | |
0 0 0 ? * 2#1 | First Monday of every month | |
0 0 23 * * ? | Every day at 11 PM |