Sub topic 4
Cron expressions and time zones
This chapter assumes you can already declare a @Scheduled method and know what @EnableScheduling does. Here you will learn how to read and write Spring's cron format and how to control which clock it uses.
Six fields, not five
Spring cron expressions have six fields, separated by spaces. Unix cron has five, so an expression copied from a crontab will not work as is.
| Position | Field | Allowed values |
|---|---|---|
| 1 | second | 0-59 |
| 2 | minute | 0-59 |
| 3 | hour | 0-23 |
| 4 | day of month | 1-31 |
| 5 | month | 1-12 or JAN-DEC |
| 6 | day of week | 0-7 or MON-SUN (0 and 7 are both Sunday) |
Spring rejects an expression with the wrong number of fields when the application context starts. A typo therefore fails at startup instead of silently never running.
Special characters
*matches every value of the field.,lists values:0 0 8,12,18 * * *runs at 08:00, 12:00 and 18:00.-gives a range:MON-FRI./gives a step:0 */15 * * * *runs every 15 minutes, and*/10in the seconds field runs every 10 seconds.?means "no specific value" in the day-of-month or day-of-week field. Spring accepts it, and it behaves like*.Lmeans the last day:0 0 0 L * *runs at midnight on the last day of each month.Wmeans the nearest weekday:15Wis the weekday closest to the 15th.#picks the nth weekday:FRI#1is the first Friday of the month.
Spring also accepts macros: @hourly, @daily, @weekly, @monthly and @yearly.
@Scheduled(cron = "0 30 9 * * MON-FRI")
public void weekdayDigest() { }
@Scheduled(cron = "0 0 0 L * *")
public void monthEndClose() { }
@Scheduled(cron = "0 0 6 * * FRI#1")
public void firstFridayReview() { }Previewing an expression
Do not wait for the clock to check a schedule. CronExpression is part of Spring and can compute the next run times directly.
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.springframework.scheduling.support.CronExpression;
public class CronPreview {
public static void main(String[] args) {
CronExpression expr = CronExpression.parse("0 30 9 * * MON-FRI");
ZonedDateTime t = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
for (int i = 0; i < 3; i++) {
t = expr.next(t);
System.out.println(t);
}
}
}The loop prints the next three weekday mornings at 09:30 Berlin time. CronExpression.parse throws IllegalArgumentException for an invalid expression.
Choosing the time zone
Without a zone, a cron trigger uses the default time zone of the JVM. That is often the server's setting, so the same code can fire at different moments on a laptop, in a container set to UTC, and on a production host. Set the zone explicitly:
@Scheduled(cron = "0 0 2 * * *", zone = "Europe/Berlin")
public void nightlyReport() { }The value must be a valid ZoneId such as Europe/Berlin or UTC. Prefer region names over fixed offsets such as +01:00, because a region name knows about daylight saving time.
Daylight saving time
Wall-clock schedules meet daylight saving time (DST) twice a year. In Europe/Berlin, clocks jump from 02:00 to 03:00 in spring, so a time between 02:00 and 02:59 does not exist that day. In autumn, the hour from 02:00 to 02:59 occurs twice. The 0 0 2 * * * job above sits exactly on the spring transition.
Two practical rules follow:
- If the job must run exactly once per day, pick a time outside the transition window, such as 04:00 for European zones.
- If the job is a technical task with no link to local business hours, use
zone = "UTC", which has no DST.
Check the transition days for your zone in a test by calling CronExpression.next with dates around the change.
Disabling a cron job
The special value - turns a cron trigger off. It is mostly used with property placeholders, so one environment can disable a job with cleanup.cron=-. The next chapter covers placeholders.
Common mistakes
- Writing five fields. Add a leading seconds field, usually
0. - Using
* * * * * *when you meant "once a minute". That expression runs every second. - Leaving the zone unset and getting different run times on different hosts.
- Setting both day-of-month and day-of-week. Use
*or?in one of them unless you have a clear reason not to.
Exercise
Write a Spring cron expression and zone for a job that runs at 18:45 on the last day of every month in New York time. Why might 0 0 2 * * * with zone Europe/Berlin be a poor choice for a once-a-day job?
Show answer
Use @Scheduled(cron = "0 45 18 L * *", zone = "America/New_York"). In Europe/Berlin, 02:00 falls in the spring DST gap, when clocks jump from 02:00 to 03:00, so that day's run may be skipped or shifted. Choose a time outside the window, such as 04:00, or use UTC.