Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2018-06-19'

解决方案:

1、局部转换

  1.  //只需要加上下面这段即可,注意不能忘记注解  
  2.     @InitBinder  
  3.     public void initBinder(WebDataBinder binder, WebRequest request) {  
  4.           
  5.         //转换日期  
  6.         DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  7.         binder.registerCustomEditor(Date.classnew CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器  
  8.     }  

2、全局转换

    创建CustomDate类实现WebBindingInitializer

  1. import java.text.DateFormat;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4. import org.springframework.beans.propertyeditors.CustomDateEditor;  
  5. import org.springframework.web.bind.WebDataBinder;  
  6. import org.springframework.web.bind.support.WebBindingInitializer;  
  7. import org.springframework.web.context.request.WebRequest;  
  8.   
  9. /** 
  10.  * @作者:JackHisen(GWD) 
  11.  * @项目名:shoppingmall 
  12.  * @时间:2017-7-12 下午10:39:10 
  13.  * @version 1.0 
  14.  */  
  15. public class CustomDate implements WebBindingInitializer{  
  16.   
  17.     @Override  
  18.     public void initBinder(WebDataBinder binder, WebRequest request) {  
  19.         // TODO Auto-generated method stub  
  20.         //转换日期  
  21.         DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  22.         binder.registerCustomEditor(Date.classnew CustomDateEditor(dateFormat, true));  
  23.     }  
  24. }  

在Spring-MVC.xml中配置日期转换

  1.  <!-- 日期转换 -->  
  2.     <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  3.         <property name="webBindingInitializer">  
  4.             <bean class="com.gwd.shopping.core.web.CustomDate"/>  
  5.         </property>  
  6.     </bean>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值