http://www.cnblogs.com/jsczljh/p/3654636.html

————————————————————————————————————————————————————————————

SpringMVC返回Json,自定义Json中Date类型格式

SpringMVC返回Json数据依赖jackson这个开源的第三方类库。

若不加任何说明情况下Date类型将以时间戳的形式转换为Json并返回。

jackson提供了一些自定义格式的方法。我们只需继承它的抽象类JsonSerializer<T> ,并在指定的属性方法上添加注解@JsonSerialize即可实现。

1.编写Date转换类(yyyy-MM-dd)

public class CustomDateSerializer extends JsonSerializer<Date>
{
    public void serialize(Date date, JsonGenerator gen, SerializerProvider provider)throws IOException, JsonProcessingException
    {
        SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = format.format(date);
        gen.writeString(formattedDate);
    }
}

 2.javabean中对应属性的get()方法添加注解

package com.easyui.model;
 
import java.util.Date;
 
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.springframework.format.annotation.DateTimeFormat;
 
import com.easyui.utils.CustomDateSerializer;
 
public class User {
    private Integer id;
 
    private String username;
 
    private String password;
 
    private String realname;
 
    private String sex;
 
    private Date birthday;
 
    public int getId() {
        return id;
    }
 
    public void setId(Integer id) {
        this.id = id;
    }
 
    public String getUsername() {
        return username;
    }
 
    public void setUsername(String username) {
        this.username = username;
    }
 
    public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getRealname() {
        return realname;
    }
 
    public void setRealname(String realname) {
        this.realname = realname;
    }
 
    public String getSex() {
        return sex;
    }
 
    public void setSex(String sex) {
        this.sex = sex;
    }
 
    @JsonSerialize(using=CustomDateSerializer.class)
    public Date getBirthday() {
        return birthday;
    }
 
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}

至此,就可以实现返回的JSON数据中日期格式自动转换为:yyyy-MM-dd 的格式

SpringMVC返回Json,自定义Json中Date类型格式的更多相关文章

  1. ORACLE中date类型字段的处理

    (1)在英文版本的ORACLE中默认日期格式为'DD-MON-YY',例如'01-JAN-98' 在汉化的中文版本中ORACLE默认日期格式为'日-月-年',例如'21-8月-2003'或'21-8月 ...

  2. MySQL中date类型的空值0000-00-00和00:00:00

    1.如果mysql中使用了date类型,并且默认值为'0000-00-00', 那么数据库中的'0000-00-00 00:00:00', '0000-00-00', '00:00:00'这三个值是相 ...

  3. Java中Date类型详解

    一.Date类型的初始化 1. Date(int year, int month, int date); 直接写入年份是得不到正确的结果的. 因为java中Date是从1900年开始算的,所以前面的第 ...

  4. 关于Java读取mysql中date类型字段默认值'0000-00-00'的问题

    今天在做项目过程中,查询一个表中数据时总碰到这个问题:      java.sql.SQLException:Value '0000-00-00' can not be represented as ...

  5. Java中Date类型如何向前向后滚动时间,( 附工具类)

    Java中的Date类型向前向后滚动时间(附工具类) 废话不多说,先看工具类: import java.text.SimpleDateFormat; import java.util.Calendar ...

  6. js中date类型的格式转化为yyyy-MM-dd HH:mm:ss的String类型

    在vue中或其他框架中可以在Date的原型链中添加Format的方法,如ruoyi可以写在main.js中更好,如果写在utils还需要去导入包. 正常的js直接放到utils.js就好 Date.p ...

  7. Java中Date类型与String 类型之间的互相转换

    Java中String类型和Date类型之间的转换 我们在注册网站的时候,往往需要填写个人信息,如姓名,年龄,出生日期等,在页面上的出生日期的值传递到后台的时候是一个字符串,而我们存入数据库的时候确需 ...

  8. mybaits中date类型显示时分秒(orcle数据库)

    <insert id="insert" parameterType="daSysLoginLog"> insert into DA_SYS_LOGI ...

  9. 如何查询mysql中date类型的时间范围记录?

    java date类型 会不会自动转换 mysql date类型? 抹除掉后面 时间 ? 时间不是查询条件?

随机推荐

  1. Office 如何下载网页的视频 JWPlayer的内嵌视频

    右击页面空白处,查看页面源代码 在里面搜索mp4或者swf,video,一般网页中的视频都是这些格式,仔细找一定能找到对应的地址 然后复制到迅雷下载即可

  2. hdu1800Flying to the Mars (字典树)

    Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the population growi ...

  3. 在xcode5下利用Source Control 做 git 项目管理

    xcode5做了很大的更新,其中一点非常实用的功能是集成了Source control项目管理,而且和git做了完美的结合:非常实用: 使用: 在新建项目时,选择 下面的 Create a git r ...

  4. vue - helloWorld

    1.cdn概念:cdn全称内容分发网络,也是加速服务之一. 2.数据绑定:{{data}} 3.el属性(挂载对象):el:标签任意(例如:#app,.app,app) 4.data:{} => ...

  5. android回调函数

    在我们进行android开发的时候,常常遇到一些回调函数,当中,我们最常常使用的回调就是,当我们对一个组件设置监听的时候,事实上就相对于设置的回调函数.比如: Button btn = (Button ...

  6. Hibernate的批量操作

    在实际的操作中,会经常的遇到批量的操作,使用hibernate将 100条记录插入到数据库的一个很自然的做法可能是这样的 Session session = sessionFactory.openSe ...

  7. maven编译war包,pom中必须有的几个dependency

    <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> ...

  8. mysql课程记录

    thread_pool可以使用Percona的版本和Mariadb的版本  都是支持的  主从切换是根据HA的方式,TDDL(Taobao Distribute Data Layer) 的方式的话,推 ...

  9. min-height clear

    在编辑页面时,总是会遇到min-height的设置 但是设置min-height后下面的div总是 跟随min-height的高度有些漂浮,如果不想让下面的div没有漂浮的效果 可以用到样式 clea ...

  10. js - 关于this、new、原型

    1.this误区 # 第三方学习 http://www.cnblogs.com/wangfupeng1988/p/3988422.html - this不是函数自身的引用,this实际上是在函数被调用 ...