alibaba/spring-cloud-alibaba · error · ParseException
Hour values must be between 0 and 23
Error message
Hour values must be between 0 and 23
What it means
Thrown in addToSet when an HOUR value (or range end) is outside 0-23, unless it is the ALL_SPEC wildcard sentinel (guard at CronExpression.java:774-777). Hours run 0-23 (midnight = 0).
Source
Thrown at spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-schedulerx/src/main/java/com/alibaba/cloud/scheduling/schedulerx/util/CronExpression.java:775
return i;
}
protected void addToSet(final int val, final int end, int incr, final int type)
throws ParseException {
final TreeSet<Integer> set = getSet(type);
if (type == SECOND || type == MINUTE) {
if ((val < 0 || val > 59 || end > 59) && (val != ALL_SPEC_INT)) {
throw new ParseException(
"Minute and Second values must be between 0 and 59",
-1);
}
}
else if (type == HOUR) {
if ((val < 0 || val > 23 || end > 23) && (val != ALL_SPEC_INT)) {
throw new ParseException(
"Hour values must be between 0 and 23", -1);
}
}
else if (type == DAY_OF_MONTH) {
if ((val < 1 || val > 31 || end > 31) && (val != ALL_SPEC_INT)
&& (val != NO_SPEC_INT)) {
throw new ParseException(
"Day of month values must be between 1 and 31", -1);
}
}
else if (type == MONTH) {
if ((val < 1 || val > 12 || end > 12) && (val != ALL_SPEC_INT)) {
throw new ParseException(
"Month values must be between 1 and 12", -1);
}
}
else if (type == DAY_OF_WEEK) {
if ((val == 0 || val > 7 || end > 7) && (val != ALL_SPEC_INT)View on GitHub (pinned to 115d590110)
Solutions
- Keep hours in 0-23; midnight is 0, e.g. '24' -> '0'.
- Convert 12-hour AM/PM input to 24-hour before building the string.
- Recount the six fields.
Example fix
// before
new CronExpression("0 0 24 * * ?");
// after
new CronExpression("0 0 0 * * ?"); // midnight daily Defensive patterns
Strategy: validation
Validate before calling
// Hours(field 2) must be 0..23.
static boolean hoursInRange(String[] fields) {
if (fields.length < 3) return false;
return inRange(fields[2], 0, 23);
}
private static boolean inRange(String f, int lo, int hi) {
if (f.equals("*") || f.equals("?")) return true;
for (String part : f.split("[-,/LW#]")) {
if (part.isEmpty()) continue;
try { int v = Integer.parseInt(part); if (v < lo || v > hi) return false; }
catch (NumberFormatException e) { /* ignore names */ }
}
return true;
} Try / catch
try {
CronExpression cron = new CronExpression(raw);
} catch (ParseException e) {
throw new IllegalArgumentException("Invalid cron expression: " + raw, e);
} Prevention
- Hours are 0-23; midnight is 0, not 24.
- Convert 12-hour/AM-PM input to 24-hour before formatting.
- Range-check hour values before building the string.
When it happens
Trigger: `new CronExpression("0 0 24 * * ?")` (hour=24), or `0 0 25 * * ?`, or a range like `0 0 0-24 * * ?`.
Common situations: Using 24 for midnight (should be 0), a 1-based/AM-PM conversion mistake, or a field-shift placing a day value into the hours field.
Related errors
AI-assisted analysis of alibaba/spring-cloud-alibaba@115d590110 (2026-08-14).
Data as JSON: /api/errors/ff624f4567245eb6.
Report an issue: GitHub.