TheAlgorithms/Java · error · IllegalArgumentException
Year must be between 46 and 8499.
Error message
Year must be between 46 and 8499.
What it means
Thrown by ZellersCongruence.parsePart (called for the year substring, positions 6-10) when the parsed year is < 46 or > 8499. This range is the algorithmic validity window for Zeller's congruence as implemented here (the constants/rounding in the formula are tuned for that span); years outside it would yield incorrect weekday results, so they are rejected rather than returning wrong answers.
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
- Constrain the year to [46, 8499] at the call site and surface the limit to users.
- For 2-digit year input, apply a pivot (e.g., +2000) before formatting.
- If you need dates outside the window, use java.time LocalDate directly instead of this util.
Example fix
// before
String input = String.format("%02d-%02d-%04d", m, d, twoDigitYear);
// after
int fullYear = twoDigitYear + 2000; // or your pivot
String input = String.format("%02d-%02d-%04d", m, d, fullYear); Defensive patterns
Strategy: validation
Validate before calling
if (year < 46 || year > 8499) throw new IllegalArgumentException("year out of [46,8499]");
String input = String.format("%02d-%02d-%04d", month, day, year);
String d = ZellersCongruence.calculateDay(input); Prevention
- Apply a century pivot to 2-digit years before formatting.
- Document the algorithm's [46,8499] validity window to users who may input ancient or far-future dates.
When it happens
Trigger: Call calculateDay("01-01-0001"), calculateDay("01-01-9000"), or any 4-digit year field outside [46, 8499].
Common situations: Two-digit years expanded incorrectly (e.g., '22' -> 22 instead of 2022), ancient-history or far-future dates, or test fixtures with year 1.
Related errors
- Month must be between 1 and 12.
- Day must be between 1 and 31.
- Input date must be 10 characters long in the format MM-DD-YY
- Invalid date.
- Bit positions must be between 0 and 31
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/14fbc8b8e44ba599.
Report an issue: GitHub.