2

I have used Gson to serialize and deserialize model class to json string , but the problem is that i have a date of birth field in model class as date as datatype which is been converted to one date before.My class contain both Date and Timestamp variables but both are serialized by the same Timestamp serializer.I have wrote both Timestamp and date serializer in my GsonBuilder.Below is my class

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;
    }
}

The gson takes the dob field to the timestamp serializer instead of date serializer and because of that the date changes to previous date.For example if date is 1992-04-18 then timestamp serializer takes it as 1992-04-18 00:00:00.0 and serialize it as 1992-04-17 17:00:00.I don't want this to happen.What i should do to restrict this for the dob field.Below is my code :

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();
}
Paul
  • 31
  • 1
  • 5
  • 1
    gson does not change the date. It only prints it in your local time zone. This may help: https://stackoverflow.com/questions/26044881/java-date-to-utc-using-gson – pschill Jun 11 '18 at 06:25
  • i want date field dob to serialize using date serializer and timestamp variable createdBy using timestamp serializer – Paul Jun 11 '18 at 06:38
  • @pschill JsonSerializer is not working.Only JsonSerializer is working ,both date and timestamp variables are taking into JsonSerializer – Paul Jun 11 '18 at 07:08

0 Answers0