{"record":{"id":"6b18a07d7848991e","repo":"apache/cassandra","slug":"unable-to-parse-the-date","errorCode":null,"errorMessage":"Unable to parse the date: ","messagePattern":"Unable to parse the date: ","errorType":"validation","errorClass":"ParseException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/ParseUtils.java","lineNumber":337,"sourceCode":"        // so we need to transform the string first\n        // so that accepted patterns are correctly handled,\n        // such as Z for UTC, or \"+00:00\" instead of \"+0000\".\n        // Note: we cannot use the X letter in the pattern\n        // because it has been introduced in Java 7.\n        str = str.replaceAll(\"(\\\\+|\\\\-)(\\\\d\\\\d):(\\\\d\\\\d)$\", \"$1$2$3\");\n        str = str.replaceAll(\"Z$\", \"+0000\");\n        ParsePosition pos = new ParsePosition(0);\n        for (String parsePattern : iso8601Patterns)\n        {\n            parser.applyPattern(parsePattern);\n            pos.setIndex(0);\n            Date date = parser.parse(str, pos);\n            if (date != null && pos.getIndex() == str.length())\n            {\n                return date;\n            }\n        }\n        throw new ParseException(\"Unable to parse the date: \" + str, -1);\n    }\n\n    /**\n     * Parse the given string as a date, using the supplied date pattern.\n     *\n     * <p>This method is adapted from Apache Commons {@code DateUtils.parseStrictly()} method (that is\n     * used Cassandra side to parse date strings)..\n     *\n     * @throws ParseException If the given string cannot be parsed with the given pattern.\n     * @see <a href=\"https://cassandra.apache.org/doc/cql3/CQL-2.2.html#usingtimestamps\">'Working with\n     * timestamps' section of CQL specification</a>\n     */\n    static Date parseDate(String str, String pattern) throws ParseException\n    {\n        SimpleDateFormat parser = new SimpleDateFormat();\n        parser.setLenient(false);\n        // set a default timezone for patterns that do not provide one\n        parser.setTimeZone(TimeZone.getTimeZone(\"UTC\"));","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/ParseUtils.java#L319-L355","documentation":"This java.text.ParseException is thrown by ParseUtils.parseDate(String, String, Locale) when none of the candidate date patterns (or the supplied pattern) can fully parse the input string, or the parser does not consume the entire string. It signals the string is not a valid date in any expected format.","triggerScenarios":"Binding a date string like '2026-13-45' or '01/02/2026x' where ParseUtils.parseDate is called with candidate patterns and every SimpleDateFormat.parse either fails or leaves unparsed trailing characters (pos.getIndex() != str.length()).","commonSituations":"US vs EU day/month order mismatches ('13/05/2026'); two-digit years resolving unexpectedly; stray whitespace or trailing characters; loading timestamps exported in a format not among the tried patterns.","solutions":["Correct the date string to match one of the supported patterns (e.g. yyyy-MM-dd or the pattern argument you passed)","Pre-validate the string with a strict regex or DateTimeFormatter.ISO_LOCAL_DATE before calling parseDate","Catch ParseException and fall back to trying alternate patterns or report the expected format to the user"],"exampleFix":"// before\nParseUtils.parseDate(\"2026/09/09\", \"yyyy-MM-dd\", Locale.US)\n// after\nParseUtils.parseDate(\"2026-09-09\", \"yyyy-MM-dd\", Locale.US)","handlingStrategy":"try-catch","validationCode":"static boolean looksLikeDate(String s, java.time.format.DateTimeFormatter f) {\n  try { java.time.LocalDate.parse(s.trim(), f); return true; } catch (java.time.format.DateTimeParseException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try { Date d = ParseUtils.parseDate(str, pattern, locale); } catch (ParseException e) { log.warn(\"Unparseable date '{}' for pattern {}\", str, pattern, e); /* fallback or rethrow as user-facing validation error */ }","preventionTips":["Trim and normalize input (strip whitespace/BOM) before parsing","Validate with java.time DateTimeFormatter first — it gives better error messages than SimpleDateFormat","Standardize on one canonical date format (ISO yyyy-MM-dd) at API boundaries","Specify an explicit Locale to avoid month-name mismatches"],"tags":["date","parsing","parse-exception"],"backgroundTag":"invalid-date-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T21:17:11.552Z"}