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类型

 

 

 

posted @ 2017-09-11 17:03  Kapa  阅读(8220)  评论(0编辑  收藏  举报