Grails saves datetime as UTC time, but reads it as local server time?

Daniel T. picture Daniel T. · Aug 31, 2012 · Viewed 9.4k times · Source

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?

Answer

Sudhir N picture Sudhir N · Aug 31, 2012

in Bootstrap.groovy

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

on JAVA_OPTS.

-Duser.timezone=UTC

Source : This thread