spring mvc get 请求 对于 “Sun Sep 29 00:28:16 CST 2019”格式日期的处理

1.首先spring mvc 没有指定  日期处理格式,比如 spring.mvc.date-format=yyyy-MM-dd HH:mm:ss 或者 其他形式的日期转化配置;

2.get请求参数为“Sun Sep 29 00:28:16 CST 2019”,controller中接收日期的类型为 java.util.Date。在

org.springframework.core.convert.support.ObjectToObjectConverter.convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType)方法中
@Nullable
public Object convert(@Nullable Object source, TypeDescriptor sourceType, TypeDescriptor targetType) {
// source="Sun Sep 29 00:28:16 CST 2019"
    if (source == null) {
        return null;
    } else {
        Class sourceClass = sourceType.getType();// java.lang.String
        Class targetClass = targetType.getType();// java.util.Data
        Member member = getValidatedMember(targetClass, sourceClass);// public java.util.Date(java.lang.String)

        try {
            if (member instanceof Method) {// false
                Method method = (Method)member;
                ReflectionUtils.makeAccessible(method);
                if (!Modifier.isStatic(method.getModifiers())) {
                    return method.invoke(source);
                }

                return method.invoke((Object)null, source);
            }

            if (member instanceof Constructor) {// true
                Constructor ctor = (Constructor)member;
                ReflectionUtils.makeAccessible(ctor);
                return ctor.newInstance(source);// 构造Date对象类似 new Date("Sun Sep 29 00:28:16 CST 2019"); --结果为--> Sun Sep 29 14:28:16 CST 2019 // 相差了12小时
            }
        } catch (InvocationTargetException var8) {
            throw new ConversionFailedException(sourceType, targetType, source, var8.getTargetException());
        } catch (Throwable var9) {
            throw new ConversionFailedException(sourceType, targetType, source, var9);
        }

        throw new IllegalStateException(String.format("No to%3$s() method exists on %1$s, and no static valueOf/of/from(%1$s) method or %3$s(%1$s) constructor exists on %2$s.", sourceClass.getName(), targetClass.getName(), targetClass.getSimpleName()));
    }
}

3.如果指定了 spring.mvc.date-format=yyyy-MM-dd HH:mm:ss,则会因为“Sun Sep 29 00:28:16 CST 2019”无法parse出现异常

你可能感兴趣的:(java,spring,mvc)