TheAlgorithms/Java · error · IllegalArgumentException

Month must be between 1 and 12.

Error message

Month must be between 1 and 12.

What it means

Thrown by ZellersCongruence.parsePart (called for the month substring, positions 0-2) when the parsed month integer is < 1 or > 12. parsePart is a generic range validator parameterized by min/max/error; for month it is invoked with bounds (1, 12).

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. Format months with a 1-based source: String.format("%02d", month1Based).
  2. Validate month in [1,12] before constructing the input string.
  3. If consuming 0-based months, add 1 before formatting.

Example fix

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

// after
String input = String.format("%02d-%02d-%04d", zeroBasedMonth + 1, day, year);
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Call calculateDay("00-15-2020") (month 0), calculateDay("13-15-2020") (month 13), or any first-two-chars value outside 01-12 that still parses as an integer.

Common situations: Zero-based month indexing leaked into a formatted string (Java's old Calendar months were 0-based), user input with month 0, or a copy-paste of MM/DD swapped with DD/MM.

Related errors


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