java时区setDefault效果 [英] java Timezone setDefault effects

查看:239
本文介绍了java时区setDefault效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下如何在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()方法将改为设置线程本地值(因此任何其他代码)在同一个线程上运行会将此值视为默认值,但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).

这篇关于java时区setDefault效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆