BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Scheduling and batch

Spring

Cron expressions in Java

A cron expression is a compact line that says when something repeats, such as every weekday at 02:30.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

A recurring-event rule in a calendar, written as five or six slots (minute, hour, day, month, weekday).

What happens, in order
  1. 1Unix cron has five fields: minute, hour, day of month, month, day of weekSpring and Quartz add a leading seconds field (six), and Quartz can add a trailing year (seven).
  2. 2Quartz requires ?in either day-of-month or day-of-week ("no specific value"); Spring accepts * in both. 0 0 2 * * ? works in Quartz, 0 0 2 * * * in Spring.
  3. 3CronExpression.parse("0 30 2 * * MON-FRI").next(ZonedDateTime.now()) (Spring) returns the next trigger time; cron-utils ExecutionTime offers the same for several dialects
  4. 4Special forms: */5 (every 5), 1,15 (a list), 1-5 (a range), L (last), MON-FRI (names), and in Spring macros such as @daily
Remember
Watch out
Pasting a Unix cron line into Spring or Quartz and getting a field shiftMissing the ?Ignoring time zones and daylight savingOverlapping runs because the job is slower than the interval
Example (highlighted lines explain it)
CronExpression cron = CronExpression.parse("0 30 2 * * MON-FRI");ZonedDateTime next = cron.next(ZonedDateTime.now(ZoneId.of("Europe/Berlin")));System.out.println("Next run: " + next);
  1. line 1 uses CronExpression, the core of Cron expressions in Java
Read the full explanation (about 2 min)

A cron expression is a compact line that says when something repeats, such as every weekday at 02:30. Java libraries read it and compute the next run times, but the dialects differ, which is a classic source of jobs running at the wrong time.

A mental model

A recurring-event rule in a calendar, written as five or six slots (minute, hour, day, month, weekday). Each calendar app reads the same slots slightly differently.

How it works

Unix cron has five fields: minute, hour, day of month, month, day of week. Spring and Quartz add a leading seconds field (six), and Quartz can add a trailing year (seven).

Quartz requires ? in either day-of-month or day-of-week ("no specific value"); Spring accepts * in both. 0 0 2 * * ? works in Quartz, 0 0 2 * * * in Spring.

CronExpression.parse("0 30 2 * * MON-FRI").next(ZonedDateTime.now()) (Spring) returns the next trigger time; cron-utils ExecutionTime offers the same for several dialects.

Special forms: */5 (every 5), 1,15 (a list), 1-5 (a range), L (last), MON-FRI (names), and in Spring macros such as @daily.

Going deeper

Time zones decide the real moment. Without zone = "..." the schedule uses the server time zone, which moves when you deploy to another region. Daylight saving changes can make 02:30 run twice or not at all on transition days.

Day-of-month and day-of-week combine with OR in classic cron (either matches) but with AND in some libraries; test the expression with the library you use.

Show the next five run times to a human before saving a schedule. That is the cheapest way to catch a wrong field order.

Under the hood

Parsers turn each field into a bit set of allowed values. To find the next time, the algorithm increments the smallest unit (seconds, then minutes, hours, days), checks it against the bit sets and carries upward, with special handling for month lengths and weekdays.

Common mistakes

  • Pasting a Unix cron line into Spring or Quartz and getting a field shift.
  • Missing the ? in Quartz.
  • Ignoring time zones and daylight saving.
  • Overlapping runs because the job is slower than the interval.

How Unravel recognises it

When you paste code, Unravel looks for these signs of cron expressions in java. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Cron expressions in Java?

A cron expression is a compact line that says when something repeats, such as every weekday at 02:30. Java libraries read it and compute the next run times, but the dialects differ, which is a classic source of jobs running at the wrong time.

What are the most common mistakes with Cron expressions in Java?

Pasting a Unix cron line into Spring or Quartz and getting a field shift. Missing the ? in Quartz. Ignoring time zones and daylight saving. Overlapping runs because the job is slower than the interval.

How can I see Cron expressions in Java in real code?

Paste your own Java into Unravel and it will point out where cron expressions in java appears, explain how it works at the detail level you choose, and draw the flow of the code around it.