alibaba/spring-cloud-alibaba · error · ParseException

'?' can only be specfied for Day-of-Month or Day-of-Week.

Error message

'?' can only be specfied for Day-of-Month or Day-of-Week.

What it means

Thrown while parsing a '?' token when the current field type is neither DAY_OF_MONTH nor DAY_OF_WEEK. The '?' modifier is only meaningful for the two day fields (it resolves the Quartz DOM-vs-DOW ambiguity); using it in seconds, minutes, hours, month, or year is illegal. ParseException message: "'?' can only be specified for Day-of-Month or Day-of-Week."

Source

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

						"Illegal characters for this position: '" + sub + "'",
						i);
			}
			if (eval != -1) {
				incr = 1;
			}
			addToSet(sval, eval, incr, type);
			return (i + 3);
		}

		if (c == '?') {
			i++;
			if ((i + 1) < s.length()
					&& (s.charAt(i) != ' ' && s.charAt(i + 1) != '\t')) {
				throw new ParseException("Illegal character after '?': "
						+ s.charAt(i), i);
			}
			if (type != DAY_OF_WEEK && type != DAY_OF_MONTH) {
				throw new ParseException(
						"'?' can only be specfied for Day-of-Month or Day-of-Week.",
						i);
			}
			if (type == DAY_OF_WEEK && !lastdayOfMonth) {
				final int val = daysOfMonth.last();
				if (val == NO_SPEC_INT) {
					throw new ParseException(
							"'?' can only be specfied for Day-of-Month -OR- Day-of-Week.",
							i);
				}
			}

			addToSet(NO_SPEC_INT, -1, 0, type);
			return i;
		}

		if (c == '*' || c == '/') {
			if (c == '*' && (i + 1) >= s.length()) {

View on GitHub (pinned to 115d590110)

Solutions

  1. Replace '?' in seconds/minutes/hours/month/year with '*' (meaning 'every value').
  2. Keep '?' only in the day-of-month or day-of-week field, and use it for exactly one of the two.
  3. Validate at config load.

Example fix

# before ('?' used in minute position)
0 ? 0 * * *

# after
0 * 0 * * ?
Defensive patterns

Strategy: try-catch

Validate before calling

static String validateCron(String cron) throws ParseException {
    new com.alibaba.cloud.scheduling.schedulerx.util.CronExpression(cron);
    return cron;
}

Type guard

// '?' only valid at field index 3 (DOM) and 5 (DOW)
static boolean qOnlyInDayFields(String[] f) {
    for (int i = 0; i < f.length; i++) {
        if ("?".equals(f[i]) && i != 3 && i != 5) return false;
    }
    return true;
}

Try / catch

try {
    new CronExpression(cron);
} catch (ParseException e) {
    // "'?' can only be specified for Day-of-Month or Day-of-Week."
    log.error("Misplaced '?' in cron '{}'", cron);
}

Prevention

When it happens

Trigger: A '?' in a non-day field, e.g. '0 ? 0 * * *' ('?' in minute) or '0 0 0 * ? *' ('?' in month). The check at CronExpression.java:391-394 fires when type != DAY_OF_WEEK && type != DAY_OF_MONTH.

Common situations: Assuming '?' is a general wildcard like '*'; converting from a system that uses '?' differently; misplacing the '?' field by one position.

Related errors


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