0

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,

6
  • Do you save this in postgres at your local machine? If not check time at server. Commented May 18, 2020 at 18:18
  • It happens in both, local and server... but I want to keep the exact same timestamp sent in the request. Commented May 18, 2020 at 18:20
  • Should you use control_initial_timestamp in place of record_insert_timestamp ? Commented May 18, 2020 at 18:28
  • record_insert_timestamp its just the name of the attribute in my json requets body. It doesn't have to do anything with what is actually happening with the value. Commented May 18, 2020 at 18:50
  • Before you persist the Timestamp, are you using Jackson or some other json marshaller to handle the POST request with the timestamp? It's possible the timezone is being offset at this point. Commented May 18, 2020 at 19:19

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried enforcing Timezones using Pheonix timezone or MST and i still get issues... all I want is to store the same date i am receiving from the request, as it is... I don't want to changed or anything.
0

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,

1 Comment

I also want to mention that to make sure I am comparing Timestamps or ZonedDates correctly, I always bring both days to the same timestamp locally or server side.
0

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

Refer below link on -- PostgreSQL timestamp

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

1 Comment

It is defined as timestamp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.