apache/dubbo · error · IllegalArgumentException

Can not cast to Date, value : '{value}'

Error message

Can not cast to Date, value : '{value}'

What it means

Thrown by DateUtils.parse(Object) when the input value is not one of the supported types (Date, Calendar, Instant, TemporalAccessor, Number, or CharSequence). The library performs a type-dispatch to coerce the value to a Date, and any type that falls outside that set cannot be converted.

Source

Thrown at dubbo-common/src/main/java/org/apache/dubbo/common/utils/DateUtils.java:136

        if (value instanceof Date) {
            return (Date) value;
        }
        if (value instanceof Calendar) {
            return ((Calendar) value).getTime();
        }
        if (value.getClass() == Instant.class) {
            return Date.from((Instant) value);
        }
        if (value instanceof TemporalAccessor) {
            return Date.from(Instant.from((TemporalAccessor) value));
        }
        if (value instanceof Number) {
            return new Date(((Number) value).longValue());
        }
        if (value instanceof CharSequence) {
            return parse(value.toString());
        }
        throw new IllegalArgumentException("Can not cast to Date, value : '" + value + "'");
    }

    public static Date parse(String value) {
        if (value == null) {
            return null;
        }
        String str = value.trim();
        int len = str.length();
        if (len == 0) {
            return null;
        }

        boolean isIso = true;
        boolean isNumeric = true;
        boolean hasDate = false;
        boolean hasTime = false;
        for (int i = 0; i < len; i++) {
            char c = str.charAt(i);

View on GitHub (pinned to 3a3043227f)

Solutions

  1. Pre-convert the value to a String, long/epoch-millis, or a known temporal type before calling DateUtils.parse
  2. Add an instanceof check against the supported set (Date, Calendar, Instant, Number, CharSequence) and handle the else branch explicitly
  3. If the value is a String in a non-standard format, register it via CUSTOM_FORMATTERS or parse it yourself and pass an Instant

Example fix

// before
Date d = DateUtils.parse(someUnknownObject);
// after
if (someUnknownObject instanceof CharSequence || someUnknownObject instanceof Number) {
    Date d = DateUtils.parse(someUnknownObject);
} else {
    throw new IllegalArgumentException("Unsupported date value: " + someUnknownObject);
}
Defensive patterns

Strategy: validation

Validate before calling

Object v = ...;
if (v == null || v instanceof java.util.Date || v instanceof java.util.Calendar
        || v instanceof java.time.temporal.TemporalAccessor || v instanceof Number
        || v instanceof CharSequence) {
    java.util.Date d = org.apache.dubbo.common.utils.DateUtils.parse(v);
} else {
    throw new IllegalArgumentException("Unsupported value type for Date parse: " + (v == null ? "null" : v.getClass()));
}

Type guard

static boolean isDateParsable(Object v) {
    return v == null || v instanceof java.util.Date || v instanceof java.util.Calendar
        || v instanceof java.time.temporal.TemporalAccessor
        || v instanceof Number || v instanceof CharSequence;
}

Try / catch

try { Date d = DateUtils.parse(value); }
catch (IllegalArgumentException e) { /* value type unsupported; convert to String/long first */ }

Prevention

When it happens

Trigger: Calling DateUtils.parse(value) where value is an arbitrary object such as a Boolean, Enum, custom POJO, or an array. The method has no catch-all conversion path, so unsupported runtime types reach the throw.

Common situations: Generic deserialization or configuration binding that pipes an unknown Object into DateUtils.parse without first narrowing the type; receiving a value from a loosely-typed Map or RPC payload.

Related errors


AI-assisted analysis of apache/dubbo@3a3043227f (2026-08-14). Data as JSON: /api/errors/bd7553e1d9e98a07. Report an issue: GitHub.