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出现异常

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值