alibaba/spring-cloud-alibaba · error · ParseException

'W' option is not valid here. (pos={})

Error message

'W' option is not valid here. (pos={})

What it means

Thrown in checkNext when 'W' (nearest weekday) follows a value but the field is not DAY_OF_MONTH (guard at CronExpression.java:543-544). 'W' is only meaningful for day-of-month ('15W' = nearest weekday to the 15th).

Source

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

					throw new ParseException("Day-of-Week values must be between 1 and 7", -1);
				}
				lastdayOfWeek = true;
			}
			else {
				throw new ParseException("'L' option is not valid here. (pos=" + i + ")", i);
			}
			final TreeSet<Integer> set = getSet(type);
			set.add(val);
			i++;
			return i;
		}

		if (c == 'W') {
			if (type == DAY_OF_MONTH) {
				nearestWeekday = true;
			}
			else {
				throw new ParseException("'W' option is not valid here. (pos=" + i + ")", i);
			}
			if (val > 31) {
				throw new ParseException("The 'W' option does not make sense with values larger than 31 (max number of days in a month)", i);
			}
			final TreeSet<Integer> set = getSet(type);
			set.add(val);
			i++;
			return i;
		}

		if (c == '#') {
			if (type != DAY_OF_WEEK) {
				throw new ParseException("'#' option is not valid here. (pos=" + i + ")", i);
			}
			i++;
			try {
				nthdayOfWeek = Integer.parseInt(s.substring(i));
				if (nthdayOfWeek < 1 || nthdayOfWeek > 5) {

View on GitHub (pinned to 115d590110)

Solutions

  1. Use '<n>W' only in the day-of-month field, e.g. '0 0 0 15W * ?'.
  2. For day-of-week, drop the 'W'; use plain numbers or 'L'/'#'.
  3. Confirm field ordering (day-of-month is the 4th field).

Example fix

// before
new CronExpression("0 0 0 ? * 6W");
// after
new CronExpression("0 0 0 15W * ?"); // nearest weekday to the 15th
Defensive patterns

Strategy: validation

Validate before calling

// 'W' is only valid in the day-of-month field.
static boolean wInDomOnly(String[] fields) {
    if (fields.length < 6) return false;
    return !fields[0].contains("W") && !fields[1].contains("W")
        && !fields[2].contains("W") && !fields[4].contains("W")
        && !fields[5].contains("W");
}

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 5W * ? *")` ('W' in hours), or `0 0 0 ? * 6W` ('W' in day-of-week). Any '<n>W' outside day-of-month.

Common situations: Developer thinks 'W' is a general 'weekday' suffix and applies it to the day-of-week field, or a missing seconds field shifts day-of-month content into another position.

Related errors


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