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

  1. Set an explicit format on the converter via setFormat(pattern) matching your input.
  2. Normalize the source to a long (epoch millis) or a Calendar before converting.
  3. 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

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


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/3aba87bc9e3e2e39. Report an issue: GitHub.