java Timezone setDefault 效果

2022-01-16 00:00:00 default timezone utc java tomcat

以下 如何在 UTC 时区存储日期/时间和时间戳使用 JPA 和 Hibernate 我想将我的应用程序默认时区设置为 UTC.我的应用程序与其他一些应用程序一起在 linux 服务器上的 tomcat 上运行.服务器还有其他运行更多应用程序的 tomcat 实例.

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.

Timezone.setDefault(tz) 会影响同一个 tomcat 实例上的其他应用吗?

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

会影响其他tomcat实例上的其他应用吗?

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.

推荐答案

java 中的默认时区设置有点奇怪.默认情况下,如果设置了默认时区,会影响整个jvm.但是,如果您使用 SecurityManager 运行,并且当前安全上下文不允许设置默认值,则 TimeZone.setDefault() 方法将改为设置 thread local 值(因此任何其他代码在同一线程上运行会将此值视为默认值,但 jvm 的其余部分将不受影响).我认为没有办法为您的应用程序"设置默认值,除非您可以将应用程序缩小到特定的线程集合(极不可能).

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 you "application" unless you can narrow your application to a specific collection of threads (highly unlikely).

相关文章