Cron Expression

About

A cron expression is a string that represents a schedule in time. It is commonly used to trigger tasks at specific times or intervals. In Spring, we can use cron expressions with the @Scheduled annotation to run methods automatically at a configured time.

Cron expressions provide fine-grained control over when a task should run, making them a powerful tool for backend scheduling.

Importance

Cron expressions are essential in scheduling for the following reasons:

  • Precise Scheduling: Define exact times, days, or months for a job to run.

  • Flexible: You can schedule tasks every second, minute, hour, or only on specific days of the week/month.

  • Externalized Config: Can be moved to properties files to make them dynamic and configurable without code changes.

  • Time Zone Support: Specify time zones for region-specific tasks.

  • Widely Supported: Cron is a standardized pattern supported by many systems including Unix/Linux, Spring, Kubernetes, and Quartz.

Syntax

Spring supports a 6-field cron expression format:

second minute hour day-of-month month day-of-week

Spring also supports a 7-field version (with optional year field), but the 6-field version is more commonly used.

Field Breakdown

Field
Allowed Values
Description

Seconds

0–59

The second of the minute

Minutes

0–59

The minute of the hour

Hours

0–23

The hour of the day (24-hour format)

Day of Month

1–31

The day of the month

Month

1–12 or JAN–DEC

The month

Day of Week

0–6 or SUN–SAT (0 = Sunday)

The day of the week

Special Characters

Symbol
Description

*

Matches all possible values for the field

?

No specific value (used in day-of-month or day-of-week to avoid conflicts)

-

Specifies a range (e.g., 10-12 means 10, 11, 12)

,

Specifies a list of values (e.g., MON,WED,FRI)

/

Specifies increments (e.g., */5 means every 5 units)

L

Last day of the month/week (Quartz only)

#

nth day of the month (e.g., MON#3 is 3rd Monday) (Quartz only)

Use ? to tell the scheduler:

"I don't care what value is in this field. Ignore it."

Don't use ? in:

  • seconds

  • minutes

  • hours

  • month

It’s valid only in:

  • day-of-month

  • day-of-week

Examples

Cron Expression
Meaning

0 * * * * *

Every minute at second 0

*/10 * * * * *

Every 10 seconds

0 */5 * * * *

Every 5 minutes

0 0 */2 * * *

Every 2 hours (at hour 0, 2, 4...)

0 0 9-17 * * *

Every hour from 9 AM to 5 PM

0 0 12 * * MON

Every Monday at 12 PM

0 0 12 ? * MON

Every Monday at noon (? avoids day-of-month ambiguity)

0 0 0 1 * ?

On the 1st day of every month at midnight

0 15 10 ? * *

Every day at 10:15 AM

0 0/30 8-18 * * MON-FRI

Every 30 minutes between 8 AM and 6 PM on weekdays

0 0 9,15,21 * * *

At 9 AM, 3 PM, and 9 PM every day

0 0 0 1 1 *

At midnight on January 1st every year

0 0 8 * * MON,WED,FRI

At 8 AM every Monday, Wednesday, and Friday

0 0 0 1,15 * *

At midnight on the 1st and 15th of every month

0 0 0 * * 0,6

At midnight every Saturday and Sunday (0=Sunday, 6=Saturday)

0 0 0 * 1,7 *

At midnight every day during January and July

0 0 0 13 * FRI

At midnight on the 13th of any month if it's a Friday

0 0/5 9-17 * * ?

Every 5 minutes between 9 AM and 5 PM every day

0 15 14 1 * ?

At 2:15 PM on the 1st of every month

0 0 12 1/2 * ?

Every 2 days starting on the 1st of the month at noon

0 0 9-17 * * MON-FRI

Every hour from 9 AM to 5 PM on Monday through Friday

0 0 6 ? * TUE,THU

Every Tuesday and Thursday at 6 AM

0 0 12 15 5 ?

At 12 PM on May 15th every year

0 0 12 * * ?

Every day at 12 PM, no specific day-of-week

0 0 9 ? * MON-FRI

Every weekday at 9 AM, ignore day-of-month

0 0 12 1 * ?

On the 1st of every month at 12 PM, ignore day-of-week

0 0 8 ? * 2

Every Tuesday at 8 AM, ignore day-of-month (2 = Tuesday)

Usage in Spring

Basic Syntax

@Scheduled(cron = "0 0 12 * * *")
public void runAtNoon() {
    // logic
}

With Time Zone

@Scheduled(cron = "0 0 7 * * *", zone = "Asia/Kolkata")
public void morningTask() {
    // logic
}

With Configuration in application.properties

myapp.scheduling.cron=0 0 23 * * *
@Scheduled(cron = "${myapp.scheduling.cron}")
public void nightlyJob() {
    // logic
}

Last updated

Was this helpful?