Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving timestamps in Postgres based on Java dates

I have a Postgres database with a table that contains a timestamp (timeOfProcessing TIMESTAMP).

I have a Java datetime value (java.util.Date dateTime) and want to store its value in that timestamp field (without time zone).

When I do it using the query

"INSERT INTO mytable(..., timeOfCreation, ...) VALUES(..., to_timestamp(" + Long.toString(dateTime.getTime()) + "),...)"

and then read the saved value (SELECT timeOfCreation FROM mytable), they are different (resultSet.getTimestamp(...).getTime() is not equal to dateTime.getTime()).

How do I need to change the insert statement in order for the datetime to be stored correctly?

like image 636
Dmitrii Pisarenko Avatar asked Aug 08 '13 17:08

Dmitrii Pisarenko


1 Answers

When inserting, instead of using (dateTime).getTime(), use an SQL timestamp object: new java.sql.Timestamp((dateTime).getTime())

like image 128
Kalaji Avatar answered Oct 10 '22 18:10

Kalaji