Grails将datetime作为UTC时间保存,但将其读取为本地服务器时间? [英] Grails saves datetime as UTC time, but reads it as local server time?
问题描述
TimeZone.setDefault(TimeZone。 getTimeZone(UTC))
我有一个审核
实体带有 dateCreated
字段:
class Audit {
日期dateCreated
字符串消息
}
然后我创建并保存一个例子:
def audit = new Audit(message:Testing audit message)
这将按照UTC时间正确保存到我的数据库。但是,当我尝试读回来:
audit = Audit.get(1)
时间戳作为本地时间读回。所以如果我的时区是+1 UTC,当前本地时间是 12:34:56 BST ,那么保存到数据库的内容是 11:34:56 ,但是当我读回来,它成为 11:34:56 BST 。有没有人知道如何解决这个问题,以便Grails会读取时间戳作为UTC并相应地转换?
code> Bootstrap.groovy
TimeZone.setDefault(TimeZone.getTimeZone(UTC ))
on JAVA_OPTS
。
-Duser.timezone = UTC
资料来源:此主题
I have the following line in my Grails application to set the default timezone to UTC:
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
I have an Audit
entity with a dateCreated
field:
class Audit {
Date dateCreated
String message
}
Then I create and save an instance of it:
def audit = new Audit(message: "Testing audit message")
This will save it to my database correctly as UTC time. However, when I try to read it back:
audit = Audit.get(1)
The timestamp is read back as local time instead. So if my timezone is +1 UTC and the current local time is 12:34:56 BST, what will be saved to the database is 11:34:56, but when I read it back, it becomes 11:34:56 BST. Does anybody know how to fix this problem so that Grails will read the timestamp back as UTC and convert it accordingly?
in Bootstrap.groovy
TimeZone.setDefault(TimeZone.getTimeZone("UTC"))
on JAVA_OPTS
.
-Duser.timezone=UTC
Source : This thread
这篇关于Grails将datetime作为UTC时间保存,但将其读取为本地服务器时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!