码迷,mamicode.com
首页 > 编程语言 > 详细

SpringMVC:Date接收String类型转换错误

时间:2015-12-02 12:16:01      阅读:1050      评论:0      收藏:0      [点我收藏+]

标签:

SpringMVC类型转换问题: String转换Date问题.

   

方法很多,就不一一罗列了.我就列举三种方法:

第一种是采用SpringMVC里面的@initBinder注解.(可以写一个全局的)

@InitBinder  

protected  void initBinder(WebDataBinder binder) {

//指定需要转换的日期格式

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    //注册日期编辑器

    //Spring还提供了很多的类型编辑器

//CustomBooleanEditorCustomNumberEditorFloatEditorDoubleEditorLongEditorCustomNumberEditorIntegerEditor

    //基本能满足开发

    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));  

}

第二种是在对应实体类属性中加上@DateTimeFormat(pattern="yyyy-MM-dd"),并且在项目中导入joda-time-2.8.1.jar。

 

第三种就是在Controller中多声明一个参数,String类型接收,然后通过一些日期工具类, 或者Calendar,SimpleDateFormat实现数据类型转换.

 技术分享

 

SpringMVC:Date接收String类型转换错误

标签:

原文地址:http://www.cnblogs.com/xash/p/5012264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!