David Brown
David Brown

Reputation: 3061

Grails issue with timezones & formatting dates

Hoping that someone can help clear up this very annoying situation I find myself in with Grails & Dates & Timezones as my understanding isn't quite complete.

To describe the situation, firstly I am in the UK, the server the app is on is in the US (CST)

Users login to my app and I have a field which holds their timezone for the sole purpose of formatting dates reliably appropriate to their location.

When the application starts up I enforce a default timezone of UTC, and I also use some Joda Dates elsewhere in the app so I set the Joda DateTimeZone also to UTC. But I only use Joda really for fixed dates that shouldn't ever change when presented.

So in Bootstrap I have TimeZone.setDefault(TimeZone.getTimeZone("UTC"))

Now to the problem, for certain tables I want to record when a user interacted so have a Date field called 'lastModified' and when the record is updated, I simply set it to a new Date()

I'm using ajax calls for presenting the data so rather than show the field directly in a GSP its being preprocessed in the controller, and for formatting I create a formatter with

DateTimeZone dtz = DateTimeZone.forID(user.timeZone)
def DateTimeFormatter tzAwareLongFormatter = DateTimeFormat.forPattern('MM/dd/yyyy HH:mm').withZone(dtz);

I thought this would be perfect, and the dates would be held UTC, then as a user with Europe/London tz I'd see the dates formatted correctly.

However the times I get are +7 Hours

So its acting like its stored the datetime according to what it would be at Server Location , even though I'm setting the default time in Bootstrap.groovy.

Is there anything I should set without having to resort to changing the server time itself?

Upvotes: 4

Views: 2157

Answers (1)

David Brown
David Brown

Reputation: 3061

It seemed that setting the default TZ in bootstrap is not enough, I had to add

-Duser.timezone=UTC 

to JAVA_OPTS, so far it appears dates are showing correctly.

Upvotes: 2

Related Questions