{"record":{"id":"3aba87bc9e3e2e39","repo":"chinabugotech/hutool","slug":"can-not-convert-to","errorCode":null,"errorMessage":"Can not convert {}:[{}] to {}","messagePattern":"Can not convert (.+?):\\[(.+?)\\] to (.+?)","errorType":"exception","errorClass":"ConvertException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/convert/impl/DateConverter.java","lineNumber":87,"sourceCode":"\t\t}\n\t\tif (value instanceof TemporalAccessor) {\n\t\t\treturn wrap(DateUtil.date((TemporalAccessor) value));\n\t\t} else if (value instanceof Calendar) {\n\t\t\treturn wrap(DateUtil.date((Calendar) value));\n\t\t} else if (value instanceof Number) {\n\t\t\treturn wrap(((Number) value).longValue());\n\t\t} else {\n\t\t\t// 统一按照字符串处理\n\t\t\tfinal String valueStr = convertToStr(value);\n\t\t\tfinal DateTime dateTime = StrUtil.isBlank(this.format) //\n\t\t\t\t\t? DateUtil.parse(valueStr) //\n\t\t\t\t\t: DateUtil.parse(valueStr, this.format);\n\t\t\tif (null != dateTime) {\n\t\t\t\treturn wrap(dateTime);\n\t\t\t}\n\t\t}\n\n\t\tthrow new ConvertException(\"Can not convert {}:[{}] to {}\", value.getClass().getName(), value, this.targetType.getName());\n\t}\n\n\t/**\n\t * java.util.Date转为子类型\n\t *\n\t * @param date Date\n\t * @return 目标类型对象\n\t */\n\tprivate java.util.Date wrap(DateTime date) {\n\t\t// 返回指定类型\n\t\tif (java.util.Date.class == targetType) {\n\t\t\treturn date.toJdkDate();\n\t\t}\n\t\tif (DateTime.class == targetType) {\n\t\t\treturn date;\n\t\t}\n\t\tif (java.sql.Date.class == targetType) {\n\t\t\treturn date.toSqlDate();","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/convert/impl/DateConverter.java#L69-L105","documentation":"DateConverter.convertInternal handles TemporalAccessor, Calendar, Number (epoch millis), and String. If the value is none of these, or String parsing via DateUtil.parse returns null, it throws ConvertException listing source class, value, and target type.","triggerScenarios":"Passing an unparseable date string (e.g. \"2022/13/45\"), an object whose toString is not a recognized date format, or a value type DateConverter does not understand.","commonSituations":"Locale-specific formats not in the default parse patterns; user input validation gaps; objects (other than Calendar/Temporal) that look like dates.","solutions":["Set an explicit format on the converter via setFormat(pattern) matching your input.","Normalize the source to a long (epoch millis) or a Calendar before converting.","Validate the string with a regex / DateTimeFormatter before calling convert."],"exampleFix":"// before\nDate d = Convert.convert(userStr, Date.class); // userStr = \"13-08-2026\"\n\n// after - explicit format\nDateConverter c = new DateConverter(Date.class);\nc.setFormat(\"dd-MM-yyyy\");\nDate d = c.convert(userStr, null);","handlingStrategy":"try-catch","validationCode":"if (value instanceof CharSequence) {\n    String s = value.toString();\n    if (DateUtil.parse(s) == null) {\n        // supply an explicit format or normalize the string\n        throw new IllegalArgumentException(\"unparseable date: \" + s);\n    }\n}\nnew DateConverter(targetType).convert(value, null);","typeGuard":null,"tryCatchPattern":"try {\n    return new DateConverter(Date.class).convert(value, null);\n} catch (ConvertException e) {\n    if (e.getMessage().startsWith(\"Can not convert\")) {\n        return defaultValue; // or retry with explicit format\n    }\n    throw e;\n}","preventionTips":["Set an explicit format on DateConverter when the input format is known.","Normalize inputs to epoch millis (long) when format is ambiguous.","Validate date strings at the boundary before conversion."],"tags":["convert","date","parsing"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}