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.