TheAlgorithms/Java · error · IllegalArgumentException
Invalid date.
Error message
Invalid date.
What it means
Thrown by ZellersCongruence.calculateDay when LocalDate.of(year, month, day) raises a DateTimeException. This is the second-stage check that catches semantically invalid dates that passed the coarse range checks: e.g., Feb 30, Apr 31, Feb 29 on a non-leap year. The original DateTimeException is chained as the cause.
Source
Thrown at src/main/java/com/thealgorithms/maths/ZellersCongruence.java:55
public static String calculateDay(String input) {
if (input == null || input.length() != 10) {
throw new IllegalArgumentException("Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY.");
}
int month = parsePart(input.substring(0, 2), 1, 12, "Month must be between 1 and 12.");
char sep1 = input.charAt(2);
validateSeparator(sep1);
int day = parsePart(input.substring(3, 5), 1, 31, "Day must be between 1 and 31.");
char sep2 = input.charAt(5);
validateSeparator(sep2);
int year = parsePart(input.substring(6, 10), 46, 8499, "Year must be between 46 and 8499.");
try {
Objects.requireNonNull(LocalDate.of(year, month, day));
} catch (DateTimeException e) {
throw new IllegalArgumentException("Invalid date.", e);
}
if (month <= 2) {
year -= 1;
month += 12;
}
int century = year / 100;
int yearOfCentury = year % 100;
int t = (int) (2.6 * month - 5.39);
int u = century / 4;
int v = yearOfCentury / 4;
int f = (int) Math.round((day + yearOfCentury + t + u + v - 2 * century) % 7.0);
int correctedDay = (f + 7) % 7;
return "The date " + input + " falls on a " + DAYS[correctedDay] + ".";
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate the date with java.time.LocalDate.of(...) yourself before calling; if it constructs, the util will accept it.
- Use a DatePicker/calendar control that disallows impossible days.
- Catch IllegalArgumentException around the call and present a user-facing 'invalid calendar date' message.
Example fix
// before String d = ZellersCongruence.calculateDay(input); // after // pre-validate so the error is yours, not the util's LocalDate.of(year, month, day); // throws DateTimeException if invalid String d = ZellersCongruence.calculateDay(input);
Defensive patterns
Strategy: validation
Validate before calling
try {
LocalDate.of(year, month, day); // throws DateTimeException if not a real calendar date
} catch (java.time.DateTimeException e) {
throw new IllegalArgumentException("Not a real calendar date", e);
}
String d = ZellersCongruence.calculateDay(input); Try / catch
try {
String d = ZellersCongruence.calculateDay(input);
} catch (IllegalArgumentException e) {
// could be format, range, OR invalid calendar date; chain has the cause
return Result.invalid(e.getMessage());
} Prevention
- Never trust a 1-31 day check alone; always validate against the actual month/leap-year via LocalDate.
- Prefer constructing the input string from a LocalDate so calendar validity is guaranteed upstream.
When it happens
Trigger: Call calculateDay("02-30-2021"), calculateDay("04-31-2020"), calculateDay("02-29-2023") (2023 is not a leap year), or any date whose day exceeds the actual days-in-month.
Common situations: Accepting day values up to 31 unconditionally (the coarse check allows 1-31 for every month), user-entered dates without calendar awareness, or test fixtures that pick round numbers.
Related errors
- Input date must be 10 characters long in the format MM-DD-YY
- Month must be between 1 and 12.
- Day must be between 1 and 31.
- Year must be between 46 and 8499.
- Invalid numeric part: {part}
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/c4648399cb753007.
Report an issue: GitHub.