Search in sources :

Example 1 with ConvertException

use of cn.hutool.core.convert.ConvertException in project hutool by looly.

the class DateConverter method convertInternal.

@Override
protected java.util.Date convertInternal(Object value) {
    if (value == null || (value instanceof CharSequence && StrUtil.isBlank(value.toString()))) {
        return null;
    }
    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());
}
Also used : TemporalAccessor(java.time.temporal.TemporalAccessor) ConvertException(cn.hutool.core.convert.ConvertException) Calendar(java.util.Calendar) DateTime(cn.hutool.core.date.DateTime)

Aggregations

ConvertException (cn.hutool.core.convert.ConvertException)1 DateTime (cn.hutool.core.date.DateTime)1 TemporalAccessor (java.time.temporal.TemporalAccessor)1 Calendar (java.util.Calendar)1