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

     

  • 相关阅读:
    json基础
    css语法以及css选择器
    HTML语义化标签
    CSS插入的四种方式
    Hibernate主键生成策略及选择
    HIbernate处理数据更新丢失
    数据库的四大特性以及事务的隔离级别
    MD5加密
    redis主从复制
    redis的持久化方案
  • 原文地址:https://www.cnblogs.com/liaohongbin/p/7505823.html
Copyright © 2020-2023  润新知