{"record":{"id":"18acb426708ca3e4","repo":"chinabugotech/hutool","slug":"unable-to-parse-the-date","errorCode":null,"errorMessage":"Unable to parse the date: {}","messagePattern":"Unable to parse the date: (.+?)","errorType":"exception","errorClass":"DateException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java","lineNumber":824,"sourceCode":"\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tcalendar.setTime(parse);\n\t\t\t\treturn calendar;\n\t\t\t}\n\n\t\t\tfinal FastDateParser fdp = new FastDateParser(parsePattern, tz, lcl);\n\t\t\tcalendar.clear();\n\t\t\ttry {\n\t\t\t\tif (fdp.parse(str, pos, calendar) && pos.getIndex() == str.length()) {\n\t\t\t\t\treturn calendar;\n\t\t\t\t}\n\t\t\t} catch (final IllegalArgumentException ignore) {\n\t\t\t\t// leniency is preventing calendar from being set\n\t\t\t}\n\t\t\tpos.setIndex(0);\n\t\t}\n\n\t\tthrow new DateException(\"Unable to parse the date: {}\", str);\n\t}\n\n\t/**\n\t * 使用指定{@link DateParser}解析字符串为{@link Calendar}\n\t *\n\t * @param str     日期字符串\n\t * @param lenient 是否宽容模式\n\t * @param parser  {@link DateParser}\n\t * @return 解析后的 {@link Calendar}，解析失败返回{@code null}\n\t * @since 5.7.14\n\t */\n\tpublic static Calendar parse(CharSequence str, boolean lenient, DateParser parser) {\n\t\tfinal Calendar calendar = Calendar.getInstance(parser.getTimeZone(), parser.getLocale());\n\t\tcalendar.clear();\n\t\tcalendar.setLenient(lenient);\n\n\t\treturn parser.parse(StrUtil.str(str), new ParsePosition(0), calendar) ? calendar : null;\n\t}","sourceCodeStart":806,"sourceCodeEnd":842,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/date/CalendarUtil.java#L806-L842","documentation":"Thrown by CalendarUtil.parseByPatterns() after the loop exhausts all provided patterns without successfully parsing the date string. Each pattern is tried with FastDateParser; if none parse the string fully (pos.getIndex() == str.length()), DateException is thrown. This means the string format does not match any supplied pattern.","triggerScenarios":"Calling parseByPatterns(\"2024-01-15T10:30:00\", \"yyyy-MM-dd\") — the time portion causes incomplete parse. Calling parseByPatterns(\"15/01/2024\", \"yyyy-MM-dd\") — day/month order mismatch. Providing patterns that do not match the locale-specific separators or field widths in the input string.","commonSituations":"Internationalization: date strings from different locales (US MM/dd/yyyy vs EU dd/MM/yyyy). User-entered dates with ambiguous formats. API responses with unexpected date formats (ISO-8601 with 'T' separator vs space). Strict (non-lenient) mode rejecting otherwise-close matches.","solutions":["Add more patterns to cover all expected input formats, e.g., include both \"yyyy-MM-dd HH:mm:ss\" and \"yyyy-MM-dd'T'HH:mm:ss\".","Verify the date string format by logging it before parsing, then match patterns accordingly.","Try lenient mode (pass true for lenient) to allow more flexible parsing.","Pre-normalize the input string (e.g., replace 'T' with space, trim, convert locale-specific month names) before parsing.","Use DateTimeFormatter or DateUtil.parse() with a known single format if the input format is deterministic."],"exampleFix":"// before\nCalendar cal = CalendarUtil.parseByPatterns(\"2024-01-15T10:30:00\", \"yyyy-MM-dd\");\n// throws: time portion not consumed\n\n// after\nCalendar cal = CalendarUtil.parseByPatterns(\"2024-01-15T10:30:00\",\n    \"yyyy-MM-dd'T'HH:mm:ss\", \"yyyy-MM-dd HH:mm:ss\", \"yyyy-MM-dd\");","handlingStrategy":"try-catch","validationCode":"// Pre-check: verify the string likely matches at least one pattern\nboolean matches = false;\nfor (String p : parsePatterns) {\n    try {\n        new SimpleDateFormat(p).parse(str);\n        matches = true; break;\n    } catch (ParseException ignored) {}\n}\nif (!matches) {\n    // add more patterns or reject\n}","typeGuard":null,"tryCatchPattern":"try {\n    return CalendarUtil.parseByPatterns(str, locale, true, patterns);\n} catch (DateException e) {\n    // try lenient mode or additional patterns\n    return CalendarUtil.parseByPatterns(str, locale, true, extraPatterns);\n}","preventionTips":["Provide multiple date patterns covering all expected input formats.","Log unparseable date strings to identify format mismatches.","Try lenient mode (true) first for more flexible parsing.","Pre-normalize input strings (trim, normalize separators) before parsing."],"tags":["date","calendar","parse","format-mismatch","patterns","date-exception"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}