spring mvc json date字符串格式化的问题

来源:互联网 发布:android卡牌游戏源码 编辑:程序博客网 时间:2024/06/05 22:46

      spring MVC 利用@ResponseBody返回json,发现date返回的值为一些毫无规律的数字,经查找在http://blog.csdn.net/yaoye_1985/article/details/16906833发现了一个方法,实践了一下,成功了,借用一下那个同志的文章,仅作一记录以备不时之需。

     public class CustomDate extends JsonSerializer<Date> {
@Override
public void serialize(Date value, JsonGenerator jgen,SerializerProvider provider)
  throws IOException,JsonProcessingException {
 SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
 String formattedDate = formatter.format(value);
 jgen.writeString(formattedDate);
}
}

    然后给实体类的要格式化的date变量的get方法上,加上@JsonSerialize(using = CustomDate.class)  。

例如:

@JsonSerialize(using = CustomDate.class)  
public Date getCreateDate() { 

    return createDate; 

}

多谢那位同志的解决方案,无意侵权,仅作记录。


0 0