Grails 将日期时间保存为 UTC 时间,但将其读取为本地服务器时间? [英] Grails saves datetime as UTC time, but reads it as local server time?

查看:15
本文介绍了Grails 将日期时间保存为 UTC 时间,但将其读取为本地服务器时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Grails 应用程序中有以下行将默认时区设置为 UTC:

I have the following line in my Grails application to set the default timezone to UTC:

TimeZone.setDefault(TimeZone.getTimeZone("UTC"))

我有一个带有 dateCreated 字段的 Audit 实体:

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")

这会将其作为 UTC 时间正确保存到我的数据库中.但是,当我尝试回读它时:

This will save it to my database correctly as UTC time. However, when I try to read it back:

audit = Audit.get(1)

时间戳被读回为本地时间.因此,如果我的时区是 +1 UTC 并且当前本地时间是 12:34:56 BST,那么将保存到数据库中的是 11:34:56,但是当我读回来时,它变成了英国夏令时 11:34:56.有谁知道如何解决这个问题,以便 Grails 将时间戳读回为 UTC 并进行相应的转换?

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?

推荐答案

Bootstrap.groovy

 TimeZone.setDefault(TimeZone.getTimeZone("UTC"))

JAVA_OPTS 上.

-Duser.timezone=UTC

来源:此线程

这篇关于Grails 将日期时间保存为 UTC 时间,但将其读取为本地服务器时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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