Joda时间DateTime错误地存储在数据库中 [英] Joda time DateTime incorrectly stores in database
问题描述
我使用 org.jadira将JodaTime
。应用服务器有+4时区。数据库服务器+9时区。 DateTime
字段存储到 timestamptz
列.usertype:usertype.jodatime:1.9 new DateTime()
结果为 $ {currentTime + 1hour} +9
其中+9是时区(正确值是 $ {currentTime的+5小时)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!).
但最好使用官方hibernate支持。它解决了开箱即用的问题,因为它使用 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屋!