Saving timestamps in Postgres based on Java dates

青春壹個敷衍的年華 提交于 2019-12-04 18:43:48

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/18132219/saving-timestamps-in-postgres-based-on-java-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!