11

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 Answer 1

21

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

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

1 Comment

@Sarief quite simply don't use an ORM. Embrace SQL and steer away from ORM monstrosities.

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.