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

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

问题描述

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

  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屋!

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