Cron expressions help you tell your Spring Boot app when to run tasks automatically. They use simple codes to set times and dates.
Cron expressions for scheduling in Spring Boot
second minute hour day-of-month month day-of-week year(optional)
Each part is a number or symbol that means when to run.
Use * to mean 'every' for that part, like every minute or every day.
0 0 8 * * ?
0 15 10 ? * MON-FRI
0 0/5 14,18 * * ?
0 0 0 1 1 ?
This Spring Boot component runs two tasks automatically. One runs daily at 8 AM, the other runs at 10:15 AM Monday to Friday. The @Scheduled annotation uses cron expressions to set the times.
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class ScheduledTasks { @Scheduled(cron = "0 0 8 * * ?") public void runDailyTask() { System.out.println("Task runs every day at 8 AM"); } @Scheduled(cron = "0 15 10 ? * MON-FRI") public void runWeekdayTask() { System.out.println("Task runs at 10:15 AM on weekdays"); } }
Use '?' in day-of-month or day-of-week to say 'no specific value' when you specify the other.
Seconds are required in Spring cron expressions, unlike some other cron systems.
Test your cron expressions with online tools to avoid mistakes.
Cron expressions tell Spring Boot when to run tasks automatically.
They use 6 or 7 parts: seconds, minutes, hours, day, month, weekday, and optional year.
Use @Scheduled annotation with cron to schedule methods easily.
Practice
Solution
Step 1: Understand the purpose of cron expressions
Cron expressions define timing details for scheduling tasks.Step 2: Identify what cron expressions control in Spring Boot
They tell Spring Boot exactly when to run a method automatically.Final Answer:
When a scheduled task should run -> Option BQuick Check:
Cron expression = schedule time [OK]
- Thinking cron sets task names
- Confusing cron with output formatting
- Assuming cron controls task priority
Solution
Step 1: Recall Spring Boot cron format
Spring Boot cron uses 6 fields: seconds, minutes, hours, day, month, weekday.Step 2: Identify correct expression for every minute
"0 * * * * *" means at 0 seconds every minute, every hour, every day.Final Answer:
@Scheduled(cron = "0 * * * * *") -> Option DQuick Check:
6-field cron with 0 seconds = every minute [OK]
- Using 5-field cron (missing seconds)
- Misplacing seconds field
- Using '?' in wrong position
"0 15 10 * * ?" in a Spring Boot @Scheduled annotation, when will the scheduled method run?Solution
Step 1: Break down the cron fields
Fields: seconds=0, minutes=15, hours=10, day=any, month=any, weekday=? (no specific day)Step 2: Interpret the timing
Means run at 10:15:00 every day, ignoring weekday.Final Answer:
Every day at 10:15 AM -> Option AQuick Check:
0 15 10 * * ? = daily 10:15 AM [OK]
- Confusing minutes and hours positions
- Ignoring '?' meaning in weekday
- Assuming it runs every hour
@Scheduled(cron = "0 0 25 * * ?")Solution
Step 1: Check each cron field's valid range
Seconds: 0-59, Minutes: 0-59, Hours: 0-23, Day: 1-31, Month: 1-12, Weekday: 1-7 or ?Step 2: Identify invalid value
Hour field is 25, which is outside 0-23 range, causing error.Final Answer:
Hour value 25 is invalid -> Option CQuick Check:
Hour must be 0-23, 25 is invalid [OK]
- Using hour > 23
- Forgetting seconds field
- Misusing '?' in day or weekday
Solution
Step 1: Understand cron fields needed
Seconds=0, Minutes=30, Hours=8, Day=? (no specific day), Month=6,7,8 (June-Aug), Weekday=MON (Monday)Step 2: Check options for correct syntax
"0 30 8 ? 6,7,8 MON" uses '?' for day, lists months as 6,7,8, and weekday as MON, which is correct.Final Answer:
"0 30 8 ? 6,7,8 MON" -> Option AQuick Check:
Use '?' for day, list months, specify MON weekday [OK]
- Using '*' for day and weekday together
- Using range '-' instead of commas for months
- Using numeric weekday instead of MON
Ready to prove it?
Pass the test and this pattern moves unknown
