zoukankan      html  css  js  c++  java
  • org.springframework.validation.BindException 日期转换异常

    因为项目需求

    要填写时间格式为yyyy-MM-dd

    而数据库的是yyyy-MM-dd   HH:mm:ss

    出现如下错误:

    1.017-09-06 15:43:57.756 [http-bio-69541-exec-8] ERROR COMMON-ERROR[28] - ""

    2.org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 3 errors

    3.Field error in object 'fund' on field 'endRaising': rejected value [2017-09-05]; codes [typeMismatch.fund.endRaising,typeMismatch.endRaising,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [fund.endRaising,endRaising]; arguments []; default message [endRaising]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endRaising'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @com.alibaba.fastjson.annotation.JSONField @javax.persistence.Column java.util.Date for value '2017-09-05'; nested exception is java.lang.IllegalArgumentException: Unable to parse '2017-09-05']

    4.Field error in object 'fund' on field 'foundTime': rejected value [2017-09-04]; codes [typeMismatch.fund.foundTime,typeMismatch.foundTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [fund.foundTime,foundTime]; arguments []; default message [foundTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'foundTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @com.alibaba.fastjson.annotation.JSONField @javax.persistence.Column java.util.Date for value '2017-09-04'; nested exception is java.lang.IllegalArgumentException: Unable to parse '2017-09-04']

    5.Field error in object 'fund' on field 'startRaising': rejected value [1970-01-21]; codes [typeMismatch.fund.startRaising,typeMismatch.startRaising,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [fund.startRaising,startRaising]; arguments []; default message [startRaising]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'startRaising'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @com.alibaba.fastjson.annotation.JSONField @javax.persistence.Column java.util.Date for value '1970-01-21'; nested exception is java.lang.IllegalArgumentException: Unable to parse '1970-01-21']

    解决办法:

        一个一个的转换

    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
    Date estab = sdf.parse(establishmenttime);//成立 时间
    fund.setFoundTime(estab);
    Date start = sdf.parse(starttime);//开始时间
    fund.setStartRaising(start);
    Date end = sdf.parse(endtime);//结束时间
    fund.setEndRaising(end);
    fund.setCompref(standard);
    fund.setFundMoneyTypeEnum(incomeType);
    Result result = fundService.saveorUpdateFundDF(fund, introFile);

    或者  前端传参数  改成    yyyy-MM-dd   HH:mm:ss 格式传入

     或者在实体类接受get set方法里面直接转换  由String接收  return  返回Date类型

     

  • 相关阅读:
    520了,用32做个简单的小程序
    sql使用手册
    大厂Redis高并发场景设计,面试问的都在这!
    如何根据普通ip地址获取当前地理位置
    理解Python闭包,这应该是最好的例子
    520了,用32做个简单的小程序
    适合 C++ 新手学习的开源项目——在 GitHub 学编程
    寄存器(内存访问)01 零基础入门学习汇编语言13
    寄存器(CPU工作原理)07 零基础入门学习汇编语言12
    数组08 零基础入门学习C语言30
  • 原文地址:https://www.cnblogs.com/liaohongbin/p/7505823.html
Copyright © 2011-2022 走看看