TheAlgorithms/Java · error · IllegalArgumentException

Date separator must be '-' or '/'.

Error message

Date separator must be '-' or '/'.

What it means

Thrown by ZellersCongruence.validateSeparator when the character at position 2 or 5 of the input is neither '-' nor '/'. The util accepts exactly two separator styles (MM-DD-YYYY or MM/DD/YYYY); any other delimiter ( '.', ' ', '\', ',') is rejected. Both separators in a single string may even differ and still pass, as long as each is one of the two allowed chars.

Source

Thrown at src/main/java/com/thealgorithms/maths/ZellersCongruence.java:104

            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

  1. Normalize separators to '-' (or '/') before calling: input.replace('.', '-').replace('/', '-').
  2. Validate the delimiter at the input layer and reject/convert early.
  3. Standardize on a single canonical format upstream of this util.

Example fix

// before
String day = ZellersCongruence.calculateDay(raw);

// after
String normalized = raw.replace('.', '-').replace(' ', '-').replace('\\', '/');
String day = ZellersCongruence.calculateDay(normalized);
Defensive patterns

Strategy: validation

Validate before calling

String normalized = input.replaceAll("[. \\\\]", "-").replace('/', '-');
String d = ZellersCongruence.calculateDay(normalized);

Prevention

When it happens

Trigger: Call calculateDay("01.01.2020"), calculateDay("01 01 2020"), calculateDay("01\01\2020"), or any input using a delimiter other than '-' or '/'.

Common situations: Locale-specific formats (ISO with '-', European with '.', US with '/'), data exported from Excel/spreadsheets using '.', or copy-paste from systems using a different delimiter.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/ee30e152717cedeb. Report an issue: GitHub.