Gson序列化器将出生日期改为前一天

时间:2018-06-11 06:13:03

标签: java json gson

我使用Gson将模型类序列化和反序列化为json字符串,但问题是我在模型类中有一个出生日期字段作为数据类型的日期,之前已转换为一个日期。我的类包含两个日期和Timestamp变量,但两者都是由相同的Timestamp序列化器序列化。我在我的GsonBuilder中写了时间戳和日期序列化器.Below是我的类

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Locale;

@Entity
@Table(name = "staff_details")
public class StaffDetails implements Serializable {

    @Id
    @GeneratedValue
    @Column(name = "id")
    private Long id;

    @Column(name="address")
    private String address;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private Integer age;

    @Column(name = "dob")
    private Date dob;

    @Column(name="created_by")
    private Long createdBy;

    @Column(name="created_on")
    private Timestamp createdOn;

    public StaffDetails() {

    }

    public StaffDetails(Long id, String address, String name, Integer age, Date dob, Long createdBy, Timestamp createdOn) {
        this.id = id;
        this.address = address;
        this.name = name;
        this.age = age;
        this.dob = dob;
        this.createdBy = createdBy;
        this.createdOn = createdOn;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Date getDob() {
        return dob;
    }

    public void setDob(Date dob) {
        this.dob = dob;
    }

    public Long getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(Long createdBy) {
        this.createdBy = createdBy;
    }

    public Timestamp getCreatedOn() {
        return createdOn;
    }

    public void setCreatedOn(Timestamp createdOn) {
        this.createdOn = createdOn;
    }
}

gson将dob字段带到时间戳序列化器而不是日期序列化器,因此日期更改为上一个日期。例如,如果日期是1992-04-18,则timestamp序列化器将其视为1992-04-18 00 :00:00.0并将其序列化为1992-04-17 17:00:00。我不希望这种情况发生。我应该怎么做才能限制dob字段。下面是我的代码:

String jsonAccts = null;
SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
SimpleDateFormat dtfDate=new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
try{
GsonBuilder builder = new GsonBuilder();

    builder.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            String jsDate = dtfDate.format(src);
            return new JsonPrimitive(jsDate);
        }
    });
    builder.registerTypeAdapter(Timestamp.class, new JsonSerializer<Timestamp>() {
        @Override
        public JsonElement serialize(Timestamp src, Type typeOfSrc, JsonSerializationContext context) {
            String jsDate = dtf.format(src);
            return new JsonPrimitive(jsDate);
        }
    });
    Gson gson = builder.create();
    Type listType = new TypeToken<List<StaffDetails>>() {}.getType();
    List<StaffDetails> staffDetailsList = new ArrayList<StaffDetails>();
    staffDetailsList = loopDao.getStaffLeaveList(customUser.getId());
    jsonAccts = gson.toJson(staffDetailsList, listType);
}catch(Exception e){
    e.printStackTrace();
}

0 个答案:

没有答案