TheAlgorithms/Java · error · IllegalArgumentException

Day must be between 1 and 31.

Error message

Day must be between 1 and 31.

What it means

Thrown by ZellersCongruence.parsePart (called for the day substring, positions 3-5) when the parsed day integer is < 1 or > 31. This is a coarse per-month-maximum check; finer per-month and leap-year validation happens later via LocalDate (producing the 'Invalid date.' error).

Source

Thrown at src/main/java/com/thealgorithms/maths/ZellersCongruence.java:88

        return "The date " + input + " falls on a " + DAYS[correctedDay] + ".";
    }

    /**
     * Parses a part of the date string and validates its range.
     *
     * @param part  the substring to parse
     * @param min   the minimum valid value
     * @param max   the maximum valid value
     * @param error the error message to throw if validation fails
     * @return the parsed integer value
     * @throws IllegalArgumentException if the part is not a valid number or is out of range
     */
    private static int parsePart(String part, int min, int max, String error) {
        try {
            int value = Integer.parseInt(part);
            if (value < min || value > max) {
                throw new IllegalArgumentException(error);
            }
            return value;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("Invalid numeric part: " + part, e);
        }
    }

    /**
     * Validates the separator character in the date string.
     *
     * @param sep the separator character
     * @throws IllegalArgumentException if the separator is not '-' or '/'
     */
    private static void validateSeparator(char sep) {
        if (sep != '-' && sep != '/') {
            throw new IllegalArgumentException("Date separator must be '-' or '/'.");
        }
    }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate day in [1,31] (and ideally against the specific month) before formatting the input string.
  2. Use LocalDate to produce the formatted string so the day is always calendar-valid.
  3. Reject day 0 explicitly at the input layer.

Example fix

// before
String input = String.format("%02d-%02d-%04d", month, rawDay, year);

// after
if (day < 1 || day > 31) throw new IllegalArgumentException("day out of range: " + day);
String input = String.format("%02d-%02d-%04d", month, day, year);
Defensive patterns

Strategy: validation

Validate before calling

if (day < 1 || day > 31) throw new IllegalArgumentException("day out of [1,31]");
String input = String.format("%02d-%02d-%04d", month, day, year);
String d = ZellersCongruence.calculateDay(input);

Prevention

When it happens

Trigger: Call calculateDay("01-00-2020") (day 0), calculateDay("01-32-2020") (day 32), or any day field outside 01-31 that is still numeric.

Common situations: Day 0 from an uninitialized/missing field, a swapped MM/DD locale producing a 'month' > 12 but a 'day' that itself is out of range, or off-by-one indexing.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/84047335848d60bd. Report an issue: GitHub.