chinabugotech/hutool · error · ConvertException
Can not convert {}:[{}] to {}
Error message
Can not convert {}:[{}] to {} What it means
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.
Source
Thrown at hutool-core/src/main/java/cn/hutool/core/convert/impl/DateConverter.java:87
}
if (value instanceof TemporalAccessor) {
return wrap(DateUtil.date((TemporalAccessor) value));
} else if (value instanceof Calendar) {
return wrap(DateUtil.date((Calendar) value));
} else if (value instanceof Number) {
return wrap(((Number) value).longValue());
} else {
// 统一按照字符串处理
final String valueStr = convertToStr(value);
final DateTime dateTime = StrUtil.isBlank(this.format) //
? DateUtil.parse(valueStr) //
: DateUtil.parse(valueStr, this.format);
if (null != dateTime) {
return wrap(dateTime);
}
}
throw new ConvertException("Can not convert {}:[{}] to {}", value.getClass().getName(), value, this.targetType.getName());
}
/**
* java.util.Date转为子类型
*
* @param date Date
* @return 目标类型对象
*/
private java.util.Date wrap(DateTime date) {
// 返回指定类型
if (java.util.Date.class == targetType) {
return date.toJdkDate();
}
if (DateTime.class == targetType) {
return date;
}
if (java.sql.Date.class == targetType) {
return date.toSqlDate();View on GitHub (pinned to 8870454b2a)
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.
Example fix
// before
Date d = Convert.convert(userStr, Date.class); // userStr = "13-08-2026"
// after - explicit format
DateConverter c = new DateConverter(Date.class);
c.setFormat("dd-MM-yyyy");
Date d = c.convert(userStr, null); Defensive patterns
Strategy: try-catch
Validate before calling
if (value instanceof CharSequence) {
String s = value.toString();
if (DateUtil.parse(s) == null) {
// supply an explicit format or normalize the string
throw new IllegalArgumentException("unparseable date: " + s);
}
}
new DateConverter(targetType).convert(value, null); Try / catch
try {
return new DateConverter(Date.class).convert(value, null);
} catch (ConvertException e) {
if (e.getMessage().startsWith("Can not convert")) {
return defaultValue; // or retry with explicit format
}
throw e;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: Locale-specific formats not in the default parse patterns; user input validation gaps; objects (other than Calendar/Temporal) that look like dates.
Related errors
- Bad number '{}{}' at: {}
- Unknown unit '{}' at: {}
- Unsupported target Date type: {}
- Unterminated quote
- (The {} locale does not support dates before 1868 AD) Unpars
AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14).
Data as JSON: /api/errors/3aba87bc9e3e2e39.
Report an issue: GitHub.