Spring team developers have done many useful improvements to Cron Schedules in Spring Boot 2.4.0 / Spring framework 5.3
Let us see some examples.
- How to Schedule a batch job for the last sunday of a month ?
Starting from Spring Boot 2.4.0 / Spring 5.3 we can use the Cron expression “0 0 0 * * SUNL”. Here the character “L” specifies Last.
@Scheduled(cron = "0 0 0 * * SUNL")
public void job(){
//dosomething
}
@Scheduled(cron = "0 0 0 * * MON#1")
public void method1(){
//dosomething
}
- How to Schedule a job for the last weekday of a month ?
In the below example L represents Last and ‘W’ represents Week Day. If we want 1st weekday then 1W is the expression.
@Scheduled(cron = "0 0 0 LW * *")
public void method1(){
//dosomething
}
More examples.
Schedule Last Friday of the Month at midnight | 0 0 0 * * 5L |
Schedule Last Day of the month at midnight | 0 0 0 L * * |
Schedule two days before the last day of the month | 0 0 0 L-2 * * |
Schedule first Friday in a month | 0 0 0 * * FRI#1 |
Schedule second Sunday in a month | 0 0 0 * * 0#2 |
Schedule first Weekday of the month | 0 0 0 1W * * |
Schedule Last Weekday of the month | 0 0 0 LW * * |
Code to find out the next run date and time from the cron expression.
In Spring 5.3 / Spring Boot 2.4.0, CronExpression replaces CronSequenceGenerator and CronSequenceGenerator is deprecated.
var expression = CronExpression.parse("0 0 0 * * FRI#1");
var result = expression.next(LocalDateTime.now());
System.out.println(result);
Spring also has some readable cron expressions (called Macros) for commonly used schedules.
@Scheduled(cron = "@hourly") //once an hour, (0 0 * * * *)
@Scheduled(cron = "@daily") //once a day (0 0 0 * * *) @midnight also does the same thing.
@Scheduled(cron = "@weekly") //once a week (0 0 0 * * 0)
@Scheduled(cron = "@monthly") //once a month (0 0 0 1 * *)
@Scheduled(cron = "@yearly") //once a year (0 0 0 1 1 *) @annually also does the same thing.
Macros also can be parsed using the CronExpression to find out the next schedule.
var expression = CronExpression.parse("@annually");
var result = expression.next(LocalDateTime.now());
System.out.println(result);
How to specify the timezone in a CRON expression in Spring ?
Say the app is setup to run in UTC timezone and if we want to set the cron trigger with Eastern timezone we can use the zone
attribute of @Scheduled
annotation as shown below in the example:
@Scheduled(cron = "0 58 21 * * THU", zone="America/New_York")
This specifies the Cron trigger to run at 9:58 PM Eastern time on Thursdays, despite the app’s timezone is set to UTC. The list of all available timezones can be found here.