13

following How to store date/time and timestamps in UTC time zone with JPA and Hibernate I want to set my application default time zone to UTC. My app runs on tomcat on a linux server along side some other apps. The server has other tomcat instances that run even more apps.

Will Timezone.setDefault(tz) affect the other apps on the same tomcat instance?

Will it affect other apps on other tomcat instances?

Is it possible to set the timezone only for the app? I loosely remember something about security manager settings to allow this.

Community
  • 1
  • 1
samz
  • 1,592
  • 3
  • 21
  • 37

2 Answers2

19

The default timezone setting in java is kind of screwy. by default, if you set the default timezone, it will affect the entire jvm. however, if you are running with a SecurityManager, and the current security context is not allowed to set the default, then the TimeZone.setDefault() method will instead set a thread local value (so any other code running on the same thread will see this value as the default, but the rest of the jvm will be unaffected). i don't think there is a way to set the default just for your "application" unless you can narrow your application to a specific collection of threads (highly unlikely).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Thanks for the detailed answer. and also awwww crap, gonna have to find a different solution to my problem... – samz Mar 27 '12 at 15:03
  • 1
    Even worse, the behavior of `setDefault` has changed with various versions of Java 4, 5, and 6. See [this answer](http://stackoverflow.com/q/2176784/642706) for details. – Basil Bourque Mar 02 '14 at 11:02
5

Would launching your app server with the java option -Duser.timezone=GMT do it for you?

ms-tg
  • 2,688
  • 23
  • 18
  • 1
    This is the correct answer if you're running your own app server. Like it or not, your JDBC driver is going to consider your machine's timezone when it reads/writes certain data types from your DB. See a couple of Mark Rotteveel's answers on the topic: http://stackoverflow.com/a/13431628/516910 and http://stackoverflow.com/a/14070771/516910 – broc.seib Apr 02 '15 at 02:27
  • I know they end up with the same result, but I'd use `UTC` rather than `GMT`. – broc.seib Apr 02 '15 at 02:30