alibaba/spring-cloud-alibaba · error · ParseException

Month values must be between 1 and 12

Error message

Month values must be between 1 and 12

What it means

Thrown in addToSet when a MONTH value (or range end) is outside 1-12, unless it is the ALL_SPEC wildcard sentinel (guard at CronExpression.java:787-790). Months are 1-based (JAN=1 ... DEC=12); note the monthMap stores 0-based internals but parsing expects 1-12 input.

Source

Thrown at spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-schedulerx/src/main/java/com/alibaba/cloud/scheduling/schedulerx/util/CronExpression.java:788

						-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)
					&& (val != NO_SPEC_INT)) {
				throw new ParseException(
						"Day-of-Week values must be between 1 and 7", -1);
			}
		}

		if ((incr == 0 || incr == -1) && val != ALL_SPEC_INT) {
			if (val != -1) {
				set.add(val);
			}
			else {
				set.add(NO_SPEC);
			}

View on GitHub (pinned to 115d590110)

Solutions

  1. Use month values 1-12 (JAN=1), e.g. '0' -> '1' or '13' -> '1'.
  2. If sourcing the month from java.util.Calendar, add 1 before formatting (Calendar months are 0-based).
  3. Recount the six fields so the month stays in position 5.

Example fix

// before
new CronExpression("0 0 0 ? 0 *");
// after
new CronExpression("0 0 0 ? 1 *"); // January
Defensive patterns

Strategy: validation

Validate before calling

// Month(field 4) must be 1..12 (ignoring '*').
static boolean monthInRange(String[] fields) {
    if (fields.length < 5) return false;
    String f = fields[4];
    if (f.equals("*")) return true;
    return inRange(f, 1, 12);
}
private static boolean inRange(String f, int lo, int hi) {
    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) { /* month names handled elsewhere */ }
    }
    return true;
}

Try / catch

try {
    CronExpression cron = new CronExpression(raw);
} catch (ParseException e) {
    throw new IllegalArgumentException("Invalid cron expression: " + raw, e);
}

Prevention

When it happens

Trigger: `new CronExpression("0 0 0 ? 0 *")` (month=0), or `0 0 0 ? 13 *` (month=13), or a range like `0 0 0 ? 1-13 *`.

Common situations: 0-based month assumption (from Java's Calendar/APIs where months are 0-11), an off-by-one, or a field-shift placing a year value into the month field.

Related errors


AI-assisted analysis of alibaba/spring-cloud-alibaba@115d590110 (2026-08-14). Data as JSON: /api/errors/5be1f01cad3f4d05. Report an issue: GitHub.