John R. Martinez
John R. Martinez

Reputation: 67

@JsonFormat Changes the Timestamp value while Serializing the Object

Currently I have a Spring application with some resources that receives different kinds of data. One of the data attributes its a Timestamp and the value is sent in the request. I am using Spring Data Jpa to persist the data in a Postgresql database.

This is how I have my object:

    @JsonProperty(value = "control_initial_timestamp")
    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    @Column(name = "ctrl_init_ts")
    private Date controlInitTimestamp;

    ... Setters and Getters ...

My Request looks like this:

    "record_insert_timestamp" : "2020-05-18 09:53:24.475"

In the database I receive this: 2020-05-18 05:53:24.475000 If you noticed, it changes the time of the whole timestamp.

Also, with Spring Data, all I am doing is object.save(objectlist); I am not doing any specially query.

Please let me know if I am missing anything.

Thanks,

Upvotes: 0

Views: 1830

Answers (3)

Rajkumar
Rajkumar

Reputation: 134

Let us know what is the datatype defined in database (timestamptz/timestamp)

Refer below link on -- PostgreSQL timestamp

https://www.postgresqltutorial.com/postgresql-timestamp/

Upvotes: 0

John R. Martinez
John R. Martinez

Reputation: 67

I got the answer. Not what I expected, but actually how it works.

Databases will store Timestamps as per their timezone location of their remote server location and timezone, to maintain a correlation between all timestamps in the DB/tables/views...etc .

When retrieving the timestamp, if you have configure your current location or timezone or remote server timezone, then it will convert to that specific timezone.

There is no direct way to manipulate this, but its actually how it works.

Thanks,

Upvotes: 0

Sudheendra
Sudheendra

Reputation: 29

Is the timezone different between your app server and database server? You can enforce timezone for date serialization in JsonFormat by passing timezone

Upvotes: 1

Related Questions