{"record":{"id":"9a106cc3dace2e29","repo":"TheAlgorithms/Java","slug":"input-date-must-be-10-characters-long-in-the-forma","errorCode":null,"errorMessage":"Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY.","messagePattern":"Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ZellersCongruence.java","lineNumber":39,"sourceCode":"\n    // Private constructor to prevent instantiation\n    private ZellersCongruence() {\n    }\n\n    /**\n     * Calculates the day of the week for a given date using Zeller's Congruence.\n     *\n     * <p>The algorithm works for both Julian and Gregorian calendar dates. The input date must be\n     * in the format \"MM-DD-YYYY\" or \"MM/DD/YYYY\".\n     *\n     * @param input the date in the format \"MM-DD-YYYY\" or \"MM/DD/YYYY\"\n     * @return a string indicating the day of the week for the given date\n     * @throws IllegalArgumentException if the input format is invalid, the date is invalid,\n     *                                  or the year is out of range\n     */\n    public static String calculateDay(String input) {\n        if (input == null || input.length() != 10) {\n            throw new IllegalArgumentException(\"Input date must be 10 characters long in the format MM-DD-YYYY or MM/DD/YYYY.\");\n        }\n\n        int month = parsePart(input.substring(0, 2), 1, 12, \"Month must be between 1 and 12.\");\n        char sep1 = input.charAt(2);\n        validateSeparator(sep1);\n\n        int day = parsePart(input.substring(3, 5), 1, 31, \"Day must be between 1 and 31.\");\n        char sep2 = input.charAt(5);\n        validateSeparator(sep2);\n\n        int year = parsePart(input.substring(6, 10), 46, 8499, \"Year must be between 46 and 8499.\");\n\n        try {\n            Objects.requireNonNull(LocalDate.of(year, month, day));\n        } catch (DateTimeException e) {\n            throw new IllegalArgumentException(\"Invalid date.\", e);\n        }\n        if (month <= 2) {","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ZellersCongruence.java#L21-L57","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\nString day = ZellersCongruence.calculateDay(rawInput);\n\n// after\nString normalized = String.format(\"%02d-%02d-%04d\", month, day, year);\nString day = ZellersCongruence.calculateDay(normalized);","handlingStrategy":"validation","validationCode":"if (input == null || input.length() != 10) {\n    throw new IllegalArgumentException(\"Date must be 10 chars: MM-DD-YYYY or MM/DD/YYYY\");\n}\nString day = ZellersCongruence.calculateDay(input);","typeGuard":null,"tryCatchPattern":"try {\n    String d = ZellersCongruence.calculateDay(input);\n} catch (IllegalArgumentException e) {\n    errors.rejectValue(\"date\", \"format.invalid\", e.getMessage());\n}","preventionTips":["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."],"tags":["date","zellers-congruence","format-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}