日期:2014-05-16  浏览次数:20530 次

Spring-JSON返回日期格式
首先是类型转换问题!
在JavaBean里字段是Date类型,对应数据库的也是Date类型,但后台和前台交互的时候,前台客户端传给后台的都是一个字符串,这时怎么办!!格式化没错,但是可以用自动格式化
//指定一个birthday进行绑定
@InitBinder  
public void initBinder(WebDataBinder binder) {   
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd");   
    binder.registerCustomEditor(Date.class, "birthday",   
    new CustomDateEditor(format, true));   
}
//对所有的Date类型进行绑定
@InitBinder  
public void initBinder(WebDataBinder binder) {   
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    binder.registerCustomEditor(Date.class,new CustomDateEditor(format, true));  
}

现在他会帮你自动转换了,ok!前>后的解决了
需要注意的是,如果这段代码在ActivityController,那么就只对Activity这个类起到作用.

后>前,我需要这个对象,获取给我!返回json
这几天遇到一个Spring在返回日期的时候格式yyyy-MM-dd HH:mm:ss(DB里的数据没问题)
取出来 2000-09-09 23:23:23.0 莫名其妙出现了一个.0