TheAlgorithms/Java · error · IllegalArgumentException
Input date must be 10 characters long in the format MM-DD-YY
Error message
Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY.
What it means
Thrown by ZellersCongruence.calculateDay when the input is null or its length is not exactly 10. The parser uses fixed substring offsets (0-2, 3-5, 6-10) and fixed charAt positions (2, 5) for separators, so a non-10-character string would throw StringIndexOutOfBoundsException; this guard fails fast with a clear message instead.
Source
Thrown at src/main/java/com/thealgorithms/maths/ZellersCongruence.java:39
// Private constructor to prevent instantiation
private ZellersCongruence() {
}
/**
* Calculates the day of the week for a given date using Zeller's Congruence.
*
* <p>The algorithm works for both Julian and Gregorian calendar dates. The input date must be
* in the format "MM-DD-YYYY" or "MM/DD/YYYY".
*
* @param input the date in the format "MM-DD-YYYY" or "MM/DD/YYYY"
* @return a string indicating the day of the week for the given date
* @throws IllegalArgumentException if the input format is invalid, the date is invalid,
* or the year is out of range
*/
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) {View on GitHub (pinned to fdfb9a395b)
Solutions
- Normalize the input to exactly 10 characters: zero-pad month/day to 2 digits and year to 4 digits, e.g., via String.format("%02d-%02d-%04d", m, d, y).
- Reject null/blank upstream with a dedicated validation error.
- Parse with a LocalDate + DateTimeFormatter first, then reformat to the expected MM-DD-YYYY shape.
Example fix
// before
String day = ZellersCongruence.calculateDay(rawInput);
// after
String normalized = String.format("%02d-%02d-%04d", month, day, year);
String day = ZellersCongruence.calculateDay(normalized); Defensive patterns
Strategy: validation
Validate before calling
if (input == null || input.length() != 10) {
throw new IllegalArgumentException("Date must be 10 chars: MM-DD-YYYY or MM/DD/YYYY");
}
String day = ZellersCongruence.calculateDay(input); Try / catch
try {
String d = ZellersCongruence.calculateDay(input);
} catch (IllegalArgumentException e) {
errors.rejectValue("date", "format.invalid", e.getMessage());
} Prevention
- Build date strings from validated ints with String.format("%02d-%02d-%04d", m, d, y) so length is guaranteed.
- Reject null/blank at the controller/form layer before numeric parsing.
When it happens
Trigger: Call calculateDay(null), calculateDay("1-1-2020"), calculateDay("01-01-20"), or any string whose length != 10 (including trailing whitespace or a missing leading zero).
Common situations: User-typed dates without zero-padding, locale-specific formats (D-M-YY), strings loaded from a CSV/DB column with inconsistent formatting, or a missing null check on optional input.
Related errors
- Invalid date.
- 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/9a106cc3dace2e29.
Report an issue: GitHub.