Cron Atlas

Quartz error A value out of range

Hour values must be between 0 and 23

java.lang.RuntimeException: CronExpression '0 0 24 * * ?' is invalid.
Caused by: java.text.ParseException: Hour values must be between 0 and 23
java.lang.RuntimeException: CronExpression '0 60 * * * ?' is invalid.
Caused by: java.text.ParseException: Minute and Second values must be between 0 and 59
java.lang.RuntimeException: CronExpression '0 0 0 0 * ?' is invalid.
Caused by: java.text.ParseException: Day of month values must be between 1 and 31
java.lang.RuntimeException: CronExpression '0 0 0 1 0 ?' is invalid.
Caused by: java.text.ParseException: Month values must be between 1 and 12

Message produced by running the expression through org.quartz.CronExpression from Quartz 2.5.2.

What it means

Each field has a fixed range, and Quartz reports the first value outside it. Seconds and minutes run from 0 to 59, hours from 0 to 23, days of the month from 1 to 31 and months from 1 to 12. Midnight is hour 0, not 24, and the first day and the first month are 1, not 0.

The value can hide inside a range or a list: 0 0 9-24 * * ? fails on the 24 just as a lone 24 would.

Rejected expressions and their fixes

Each expression opens in the tester. The message under a rejected one is what Quartz 2.5.2 printed for it.

  1. Rejected0 0 24 * * ?Hour values must be between 0 and 23
    Use
    0 0 0 * * ?At 00:00.

    Midnight every day.

  2. Rejected0 0 9-24 * * ?Hour values must be between 0 and 23
    Use
    0 0 9-23 * * ?On the hour from 09:00 through 23:00.

    On the hour from 09:00 to 23:00.

  3. Rejected0 60 * * * ?Minute and Second values must be between 0 and 59
    Use
    0 0 * * * ?Every hour, on the hour.

    Every hour on the hour. Minute 60 would be minute 0 of the next hour.

  4. Rejected0 0 0 0 * ?Day of month values must be between 1 and 31
    Use
    0 0 0 1 * ?At 00:00, on the 1st of the month.

    Midnight on the 1st of the month.

  5. Rejected0 0 0 1 0 ?Month values must be between 1 and 12
    Use
    0 0 0 1 1 ?At 00:00, on the 1st of the month, in January.

    Midnight on 1 January. Months count from 1, and JAN works too.

Questions

Is midnight 0 or 24 in a Quartz cron expression?
0. The hour field runs from 0 to 23, so midnight every day is 0 0 0 * * ?.
How do I run on the last day of the month?
Use L in day of month: 0 0 0 L * ?. A plain 31 is valid but only fires in months that have 31 days.

Other Quartz errors

Reference