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

     

  • 相关阅读:
    安卓使用spinner控件和pull解析实现全国省市县的三级联动(附上xml文件)
    安卓linearlayout布局的一个嵌套实例
    接口回调的例子和安卓中的接口回掉实例
    Android Studio 快捷键
    java比较器 之compareable 和comparato比较
    4.Gradle构建Spring Boot项目
    2.Gradle安装和常用命令
    1.Gradle基础介绍
    6.SpringBoot学习(六)——Spring Boot Banner自定义
    4.SpringBoot学习(四)——Spring Boot Validation校验及原理
  • 原文地址:https://www.cnblogs.com/liaohongbin/p/7505823.html
Copyright © 2011-2022 走看看