material-components/material-components-android · error · IllegalArgumentException

firstDayOfWeek is not valid

Error message

firstDayOfWeek is not valid

What it means

CalendarConstraints requires firstDayOfWeek to be within 0..getMaximum(DAY_OF_WEEK) (typically 7, and 0 means 'use locale default'). Values outside that range throw IllegalArgumentException in the constructor, because the picker grids cannot be laid out with an invalid week-day index.

Source

Thrown at lib/java/com/google/android/material/datepicker/CalendarConstraints.java:81

      @Nullable Month openAt,
      int firstDayOfWeek) {
    Objects.requireNonNull(start, "start cannot be null");
    Objects.requireNonNull(end, "end cannot be null");
    Objects.requireNonNull(validator, "validator cannot be null");
    this.start = start;
    this.end = end;
    this.openAt = openAt;
    this.firstDayOfWeek = firstDayOfWeek;
    this.validator = validator;
    if (openAt != null && start.compareTo(openAt) > 0) {
      throw new IllegalArgumentException("start Month cannot be after current Month");
    }
    if (openAt != null && openAt.compareTo(end) > 0) {
      throw new IllegalArgumentException("current Month cannot be after end Month");
    }
    if (firstDayOfWeek < 0
        || firstDayOfWeek > UtcDates.getUtcCalendar().getMaximum(Calendar.DAY_OF_WEEK)) {
      throw new IllegalArgumentException("firstDayOfWeek is not valid");
    }
    monthSpan = start.monthsUntil(end) + 1;
    yearSpan = end.year - start.year + 1;
  }

  boolean isWithinBounds(long date) {
    return start.getDay(1) <= date && date <= end.getDay(end.daysInMonth);
  }

  /**
   * Returns the {@link DateValidator} that determines whether a date can be clicked and selected.
   */
  public DateValidator getDateValidator() {
    return validator;
  }

  /** Returns the earliest month allowed by this set of bounds. */
  @NonNull

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Validate and clamp: use 0 for locale default, else ensure 1..7 before setFirstDayOfWeek.
  2. Check the mapping of your configuration scheme (ISO-8601 1=Monday vs java.util.Calendar 1=Sunday) to the picker's expectation.
  3. Default to not calling setFirstDayOfWeek at all when the value is unknown.

Example fix

// before
val constraints = CalendarConstraints.Builder()
    .setFirstDayOfWeek(configStartOfWeek) // e.g. 8 from server config -> throws
    .build()

// after
val fdow = configStartOfWeek.coerceIn(0, 7)
val constraints = CalendarConstraints.Builder()
    .setFirstDayOfWeek(fdow)
    .build()
Defensive patterns

Strategy: validation

Validate before calling

fun Int.toValidFirstDayOfWeek(): Int = coerceIn(0, 7) // 0 = locale default, 1..7 valid

val constraints = CalendarConstraints.Builder()
    .setFirstDayOfWeek(configFdow.toValidFirstDayOfWeek())
    .build()

Prevention

When it happens

Trigger: Builder().setFirstDayOfWeek(v) with v < 0 or v > 7 (e.g. passing Calendar.DAY_OF_WEEK constants like 8+, or a config value parsed as 8+); forwarding an unvalidated server/local-storage setting into the builder.

Common situations: Remote-config-driven first-day-of-week (1=Mon ... 7=Sun schemes) mapped incorrectly onto the API's expected range; locale-aware code computing the value from Calendar.getFirstDayOfWeek but mixing in a default of -1 for 'unset'.

Related errors


AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14). Data as JSON: /api/errors/2dc987f5de747423. Report an issue: GitHub.