{"record":{"id":"84047335848d60bd","repo":"TheAlgorithms/Java","slug":"day-must-be-between-1-and-31","errorCode":null,"errorMessage":"Day must be between 1 and 31.","messagePattern":"Day must be between 1 and 31\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/ZellersCongruence.java","lineNumber":88,"sourceCode":"\n        return \"The date \" + input + \" falls on a \" + DAYS[correctedDay] + \".\";\n    }\n\n    /**\n     * Parses a part of the date string and validates its range.\n     *\n     * @param part  the substring to parse\n     * @param min   the minimum valid value\n     * @param max   the maximum valid value\n     * @param error the error message to throw if validation fails\n     * @return the parsed integer value\n     * @throws IllegalArgumentException if the part is not a valid number or is out of range\n     */\n    private static int parsePart(String part, int min, int max, String error) {\n        try {\n            int value = Integer.parseInt(part);\n            if (value < min || value > max) {\n                throw new IllegalArgumentException(error);\n            }\n            return value;\n        } catch (NumberFormatException e) {\n            throw new IllegalArgumentException(\"Invalid numeric part: \" + part, e);\n        }\n    }\n\n    /**\n     * Validates the separator character in the date string.\n     *\n     * @param sep the separator character\n     * @throws IllegalArgumentException if the separator is not '-' or '/'\n     */\n    private static void validateSeparator(char sep) {\n        if (sep != '-' && sep != '/') {\n            throw new IllegalArgumentException(\"Date separator must be '-' or '/'.\");\n        }\n    }","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/ZellersCongruence.java#L70-L106","documentation":"Thrown by ZellersCongruence.parsePart (called for the day substring, positions 3-5) when the parsed day integer is < 1 or > 31. This is a coarse per-month-maximum check; finer per-month and leap-year validation happens later via LocalDate (producing the 'Invalid date.' error).","triggerScenarios":"Call calculateDay(\"01-00-2020\") (day 0), calculateDay(\"01-32-2020\") (day 32), or any day field outside 01-31 that is still numeric.","commonSituations":"Day 0 from an uninitialized/missing field, a swapped MM/DD locale producing a 'month' > 12 but a 'day' that itself is out of range, or off-by-one indexing.","solutions":["Validate day in [1,31] (and ideally against the specific month) before formatting the input string.","Use LocalDate to produce the formatted string so the day is always calendar-valid.","Reject day 0 explicitly at the input layer."],"exampleFix":"// before\nString input = String.format(\"%02d-%02d-%04d\", month, rawDay, year);\n\n// after\nif (day < 1 || day > 31) throw new IllegalArgumentException(\"day out of range: \" + day);\nString input = String.format(\"%02d-%02d-%04d\", month, day, year);","handlingStrategy":"validation","validationCode":"if (day < 1 || day > 31) throw new IllegalArgumentException(\"day out of [1,31]\");\nString input = String.format(\"%02d-%02d-%04d\", month, day, year);\nString d = ZellersCongruence.calculateDay(input);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pair the 1-31 coarse check with a LocalDate-based per-month check.","Reject day 0 explicitly at the form/parsing layer."],"tags":["date","zellers-congruence","range-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}