根据Java日期在Postgres中保存时间戳
我有一个Postgres数据库,其中包含一个包含时间戳的表( timeOfProcessing TIMESTAMP
)。
我有一个Java日期时间值( java.util.Date dateTime
),并希望将其值存储在该时间戳字段中(没有时区)。
当我使用查询时
"INSERT INTO mytable(..., timeOfCreation, ...) VALUES(..., to_timestamp(" + Long.toString(dateTime.getTime()) + "),...)"
然后读取保存的值( SELECT timeOfCreation FROM mytable
),它们是不同的( resultSet.getTimestamp(...).getTime()
不等于dateTime.getTime()
)。
我如何更改insert语句以便正确存储日期时间?
插入时,不使用(dateTime).getTime()
,而是使用SQL时间戳对象: new java.sql.Timestamp((dateTime).getTime())