Joda 时间 DateTime 错误地存储在数据库中 [英] Joda time DateTime incorrectly stores in database
问题描述
我正在使用 org.jadira.usertype:usertype.jodatime:1.9
将 JodaTime DateTime
字段存储到 timestamptz
列.应用服务器有 +4 时区.数据库服务器 +9 时区.new DateTime()
结果为 ${currentTime+1hour}+9
其中 +9 是时区(正确值是 ${currentTime+5hours)+9代码>).
I'm storing JodaTime DateTime
field to timestamptz
column by using org.jadira.usertype:usertype.jodatime:1.9
. App server has +4 time zone. DB server +9 time zone. new DateTime()
results in ${currentTime+1hour}+9
where +9 is time zone (correct value is ${currentTime+5hours)+9
).
我没有找到任何相关主题.java.util.Date
正确存储.
I haven't found any related topics. java.util.Date
stores correctly.
域对象具有以下映射属性:
Domain object has the following mapping property:
static mapping = {
dateCreated sqlType:'timestamptz'
}
如何正确存储 DateTime?
How can I store DateTime correctly?
推荐答案
好的.我已经花了 8 个小时来解决这个问题.如果您使用 usertype 项目来持久化 JodaTime,则必须设置 databaseZone
属性PersistentDateTime
类等于当前应用服务器时区(不是数据库!).
Ok. I have spend 8 hours to solve the problem. If your are using usertype project to persist JodaTime, you have to set databaseZone
property of PersistentDateTime
class equals to current application server timezone (not database!).
但最好使用官方的休眠支持.它解决了开箱即用的问题,因为它使用 java.utl.Date
来持久化 DateTime
和 java.util.Date
默认情况下正确持久化
But it's better to use official hibernate support. It solves the problem out of the box because it uses java.utl.Date
to persist DateTime
and java.util.Date
correctly persisted by default
这篇关于Joda 时间 DateTime 错误地存储在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!