首页 > 编程语言 > 详细

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

时间:2015-12-02 12:14:22      阅读:311      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!