1

In my bootstrap.groovy I'm adding:

TimeZone.setDefault(TimeZone.getTimeZone('America/Detroit'))

This works just fine when running my app locally; it changes the timezone correctly.

But when I deploy my app to my VPS, which is running CentOS Linux 6.5, the time is offset by three hours. Note: It is increased by three hours.

Also, I am running my app on Tomcat 7. I've tried adding:

export JAVA_OPTS="-Duser.timezone=America/Detroit"

to my setenv.sh file but this does not seem to work.

My server's system time and hardware time is correctly set and working. So I have NO idea why Tomcat is adding three hours to my dates!

Any help or guidance is appreciated!

sean3838
  • 65
  • 8

1 Answers1

1

You do not say:

  • what your operating system's settings are
  • what your JVM's default settings are
  • how you are getting the supposedly wrong date-time values
  • if you bothered to verify the correct clock time on the computer.

So it is difficult to diagnose.

Also, this approach is ill-advised. Using code to alter the default time zone of your JVM affects all code running in all threads of the JVM. So you are changing the execution of code as it runs.

Generally your servers should be set to UTC, or if not possible, then set to Reykjavík Iceland. Your business code and storage should be in UTC, with date-time values adjusted to a user's own locality (or expectations) only upon presentation in the user-interface. Most importantly, your code should always specify the desired time zone rather than depend on the JVM's default.

Hopefully you know to avoid the old java.util.Date and .Calendar classes bundled in Java, as they are notoriously troublesome. Instead use either Joda-Time to the new java.time in Java 8. Both frameworks have clear clean support of time zones. Search StackOverflow for many examples.

Tip: Verify your server's clock at runtime by contacting a time server locally or on the internet to get current time. Then compare to the server's time. If too far apart then log an error or throw exception, etc.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • Thanks. I'm going to use UTC format throughout my app. I posted a new problem though that arises from this here: http://stackoverflow.com/questions/24898020/grails-java-new-date-different-in-gsp – sean3838 Jul 23 '14 at 13:42