根据Java日期在Postgres中保存时间戳 [英] Saving timestamps in Postgres based on Java dates

查看:899
本文介绍了根据Java日期在Postgres中保存时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Postgres数据库,其中包含一个包含时间戳的表( timeOfProcessing TIMESTAMP )。

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

我有Java日期时间值( java.util.Date dateTime )并希望将其值存储在该时间戳字段中(不带时区)。

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

当我使用查询时

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

然后读取保存的值( SELECT timeOfCreation FROM mytable ),它们是不同的( resultSet.getTimestamp(...)。getTime()不等于 dateTime.getTime())。

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

我如何更改? insert语句是为了正确存储日期时间吗?

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

推荐答案

插入时,而不是使用 (dateTime).getTime(),使用SQL时间戳对象: new java.sql.Timestamp((dateTime).g etTime())

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

这篇关于根据Java日期在Postgres中保存时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆