SpringMVC实体类时间格式化

后台一般给前端提供的时间都是时间戳,这样方于前端灵活处理各种时间格式,但有时候需要通过后台来进行统一格式化。
后台时间格式化:
1.添加jar包:

<dependency>
 <groupId>com.fasterxml.jackson.coregroupId>
    <artifactId>jackson-databindartifactId>
    <version>${jackson.version}version>
dependency>

2.实体类:
获取时间的方法添加如下注解:
@JsonFormat(pattern=”yyyy-MM-dd HH:mm:ss”,timezone = “GMT+8”)
如:

package com.linxz.android.pojo;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;


public class BaseUser extends BaseUserKey {
    private String name;

    private String pwd;
    private String phone;
    private String email;
    private String adress;
    private Date registerTime;
    private Date loginTime;

    private String sex;

    private String role;

    private String token;

    private String stattus;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd == null ? null : pwd.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email == null ? null : email.trim();
    }

    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress == null ? null : adress.trim();
    }

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role == null ? null : role.trim();
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token == null ? null : token.trim();
    }

    public String getStattus() {
        return stattus;
    }

    public void setStattus(String stattus) {
        this.stattus = stattus == null ? null : stattus.trim();
    }
}

你可能感兴趣的:(java开发)