Brodiman
Brodiman

Reputation: 159

Serialized Timestamps converting to seconds

I'm trying to serialize a Timestamp Object to a JSON. But the object in the JSON is displayed as seconds.

This is a snippet of my POJO:

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class TimeAndDateDetail{

    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd hh.mm.ss")
    private Timestamp timeAndDate;

    public Timestame getTimeAndDate() {return timeAndDate; }

    public void setTimeAndDate(Timestamp timeAndDate){
        this.timeAndDate = timeAndDate;
    }
}

This is my output:

{
    "timeAndDate": 1583038800000
}

Why is this happening? And how can I get it to keep its original format?

Upvotes: 0

Views: 555

Answers (2)

Kemal Erbakirci
Kemal Erbakirci

Reputation: 173

Looks like you are using jackson, and this is the default behaviour of it. best way is to disable the related object mapper feature:

objectMapper
    .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)

Upvotes: 0

Andronicus
Andronicus

Reputation: 26046

You can annotate the field with @JsonFormat to specify the format, that the timestamp will be serialized. Here is an example:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm")

Upvotes: 1

Related Questions