Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for pr

问题描述:使用My97DatePicker插件将前台时间数据传递给后台出现字符串转Date格式问题。

以下为报错信息:

Field error in object 'TWeekConfig' on field 'deptLineTime': rejected value [05:05]; codes [typeMismatch.TWeekConfig.deptLineTime,typeMismatch.deptLineTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [TWeekConfig.deptLineTime,deptLineTime]; arguments []; default message [deptLineTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'deptLineTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '05:05'; nested exception is java.lang.IllegalArgumentException: Unable to parse '05:05']
Field error in object 'TWeekConfig' on field 'userLineTime': rejected value [7:00]; codes [typeMismatch.TWeekConfig.userLineTime,typeMismatch.userLineTime,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [TWeekConfig.userLineTime,userLineTime]; arguments []; default message [userLineTime]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'userLineTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @org.springframework.format.annotation.DateTimeFormat java.util.Date for value '7:00'; nested exception is java.lang.IllegalArgumentException: Unable to parse '7:00']

主要问题就是数据格式错误,这个bug问题花了好长时间,最后解决却发现非常简单,使用注解便可解决,将注解中时间格式更改即可。

先将有Bug的代码放上:

界面代码:

 <div class="form-group">
    <label class="control-label col-md-1">部门提交到公司汇总截止时间点</label>
      <div class="col-md-3">
         <div class="input-icon right">
            <input type="text" id="deptLineTime" onclick="WdatePicker({skin:'whyGreen',dateFmt:'H:mm:ss'})" class="form-control" autocomplete="off" name="deptLineTime" placeholder="请输入时间点(不可早于个人提交时间点)">
          </div>
       </div>
 </div>

<div class="form-group">
    <label class="control-label col-md-1">个人提交到公司汇总截止时间点</label>
         <div class="col-md-3">
           <div class="input-icon right">
               <input type="text" id="userLineTime" class="form-control" onclick="WdatePicker({skin:'whyGreen',dateFmt:'H:mm:ss'})" autocomplete="off" name="userLineTime" placeholder="请输入时间点(不可晚于部门提交时间点)">
           </div>
         </div>
  </div>

注意,前端收到的时间格式为时间点(HH:MM:SS),所以与其他的格式不同,这点一开始没有注意到,才耽误了好多时间。

接下来是对应的实体类。可见注解(SpringMVC)中显示的是年月格式。

   /**提交至部门截止天数*/
	private Integer userLineDay;

    /**提交至公司截止时间*/
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date userLineTime;

    /**提交至公司截止天数*/
	private Integer deptLineDay;

    /**提交至部门截止时间*/
	@DateTimeFormat(pattern = "yyyy-MM-dd")
	private Date deptLineTime;
	

应该更改注解格式为时间点就好,修改后的代码。

  /**提交至部门截止天数*/
	private Integer userLineDay;

    /**提交至公司截止时间*/
	@DateTimeFormat(pattern = "HH:mm:ss")
	private Date userLineTime;

    /**提交至公司截止天数*/
	private Integer deptLineDay;

    /**提交至部门截止时间*/
	@DateTimeFormat(pattern = "HH:mm:ss")
	private Date deptLineTime;

这次问题解决。

感谢在解决这次问题中索搜到的这及篇内容不错的博客:

SpringMVC 处理Date类型@DateTimeFormat @InitBinder

springmvc/springboot处理前台字符串日期自动转换成后台date类型的三种办法

前台字符串到后台Date的转换

一起学习,共同成长。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值