Master Spring Boot Cron Expressions: New Macros, Last-Day & Weekday Features

Learn how Spring Boot 2.4 replaces the old CronSequenceGenerator with CronExpression, enabling advanced scheduling features such as built-in macros, last-day-of-month, weekday, and week-of-month semantics, with practical code examples and a reference table of common macros.

Java Architecture Diary
Java Architecture Diary
Java Architecture Diary
Master Spring Boot Cron Expressions: New Macros, Last-Day & Weekday Features

Cron expressions are widely used for defining scheduled task execution rules. In Spring Boot, they are typically used with the @Scheduled annotation. Starting with Spring Boot 2.4 (Spring Framework 5.3), the old CronSequenceGenerator based on java.util.Calendar is replaced by the more powerful CronExpression processor, which can handle semantics like the last day of the month.

Using CronExpression to Calculate Next Execution Time

LocalDateTime now = LocalDateTime.now();
System.out.println("Current time: " + now);
// Every hour
CronExpression expression1 = CronExpression.parse("0 0 0/1 * * *");
LocalDateTime nextTime = expression1.next(now);
System.out.println("Every hour -> next execution: " + nextTime);
// Every hour at minute 10
CronExpression expression2 = CronExpression.parse("0 10 0/1 * * *");
LocalDateTime nextTime2 = expression2.next(now);
System.out.println("Every hour at minute 10 -> next execution: " + nextTime2);

Sample output:

Current time: 2020-11-14T23:04:46.302739
Every hour -> next execution: 2020-11-15T00:00
Every hour at minute 10 -> next execution: 2020-11-14T23:10

New Common Expression Macros

For non‑developers cron expressions are hard to understand, so Spring Boot now supports macros that represent common schedules, allowing you to use them instead of six‑field expressions.

@Scheduled(cron = "@hourly")

which is equivalent to

@Scheduled(cron = "0 0 * * * *")

Other Common Macro Commands

@yearly – 0 0 0 1 1 * – runs once a year

@monthly – 0 0 0 1 * * – runs once a month

@weekly – 0 0 0 * * 0 – runs once a week

@daily or @annually – 0 0 0 * * * – runs once a day

@hourly – 0 0 * * * * – runs once an hour

Enhanced Expressions – Last Days of Month

The “day‑of‑month” field now supports the “L” (last) syntax.

0 0 0 L * *   // midnight on the last day of the month
0 0 0 L-3 * * // midnight on the third‑last day of the month
0 0 0 * * 5L  // midnight on the last Friday of the month
0 0 0 * * FRIL // same using English weekday abbreviation

Enhanced Expressions – Weekdays (W)

The “day‑of‑month” field also supports the “W” (weekday) syntax.

0 0 0 1W * * // midnight on the first weekday of the month
0 0 0 LW * * // midnight on the last weekday of the month

Enhanced Expressions – Nth Weekday of the Month

You can specify the Nth occurrence of a weekday in a month using “#”.

0 0 0 ? * 5#2   // second Friday of the month at midnight
0 0 0 ? * MON#1 // first Monday of the month at midnight

Reference

Online testing tool: https://cron.qqe2.com/

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentschedulingSpring BootCron Expression
Java Architecture Diary
Written by

Java Architecture Diary

Committed to sharing original, high‑quality technical articles; no fluff or promotional content.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.