chinabugotech/hutool · error · UnsupportedOperationException

Unsupported target Date type

Error message

Unsupported target Date type: {}

What it means

DateConverter.wrap(DateTime) only returns java.util.Date, DateTime, java.sql.Date, java.sql.Time, java.sql.Timestamp. Any other java.util.Date subclass as targetType throws UnsupportedOperationException.

Solutions

  1. Target one of the supported subtypes (typically java.util.Date) and adapt afterwards.
  2. Register a custom Converter for your date subtype.
  3. Convert to java.util.Date then construct your subtype from getTime().

Example fix

// before
DateConverter c = new DateConverter(MyDate.class);

// after - target base type then adapt
Date base = new DateConverter(Date.class).convert(value, null);
MyDate md = new MyDate(base.getTime());
Defensive patterns

Strategy: validation

Validate before calling

private static final Set<Class<?>> SUPPORTED = Set.of(
        java.util.Date.class, DateTime.class,
        java.sql.Date.class, java.sql.Time.class, java.sql.Timestamp.class);
if (!SUPPORTED.contains(targetType)) {
    targetType = java.util.Date.class; // fall back to base type
}
new DateConverter(targetType);

Try / catch

try {
    return new DateConverter(targetType).convert(value, null);
} catch (UnsupportedOperationException e) {
    if (e.getMessage().startsWith("Unsupported target Date type")) {
        return new DateConverter(java.util.Date.class).convert(value, null);
    }
    throw e;
}

Prevention

When it happens

Trigger: Constructing new DateConverter(MyCustomDate.class) where MyCustomDate extends java.util.Date but is not one of the five supported subtypes; converting from a TemporalAccessor/Calendar/DateTime source.

Common situations: Using third-party date types (e.g. Joda DateTime that extends java.util.Date), custom date wrappers.

Related errors


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

Appendix: source

Thrown at hutool-core/src/main/java/cn/hutool/core/convert/impl/DateConverter.java:114

	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();
		}
		if (java.sql.Time.class == targetType) {
			return new java.sql.Time(date.getTime());
		}
		if (java.sql.Timestamp.class == targetType) {
			return date.toTimestamp();
		}

		throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", this.targetType.getName()));
	}

	/**
	 * java.util.Date转为子类型
	 *
	 * @param mills Date
	 * @return 目标类型对象
	 */
	private java.util.Date wrap(long mills) {
		if(GlobalCustomFormat.FORMAT_SECONDS.equals(this.format)){
			// Unix时间戳
			return DateUtil.date(mills * 1000);
		}

		// 返回指定类型
		if (java.util.Date.class == targetType) {
			return new java.util.Date(mills);
		}

View on GitHub (pinned to 8870454b2a)