JDZ6中的TimeZone.setDefault更改 [英] TimeZone.setDefault changes in JDK6

查看:169
本文介绍了JDZ6中的TimeZone.setDefault更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚才注意到JDK 6设置默认TimeZone的方法与JDK5不同。

I just noticed that JDK 6 has a different approach to setting a default TimeZone than JDK5.

以前,新的默认值将存储在线程局部变量中。使用JDK6(我刚刚查看了1.6.0.18),实现已经改变,因此如果用户可以写入user.timezone属性,或者如果没有安装SecurityManager,则时区会在整个VM范围内发生变化!否则会发生线程局部更改。

Previously the new default would be stored in a thread-local variable. With JDK6 (I just reviewed 1.6.0.18) the implementation has changed, so that if the user can write to the "user.timezone" property, or if there is no SecurityManager installed, the timezone changes VM-wide! Otherwise a thread-local change occurs.

我错了吗?这似乎是一个相当大的变化,我在网上找不到任何关于它的东西。

Am I wrong? This seems to be quite a drastic change, and I couldn't find anything on the web about it.

这是JDK6代码:

 private static boolean hasPermission() {
  boolean hasPermission = true;
  SecurityManager sm = System.getSecurityManager();
  if (sm != null) {
   try {
    sm.checkPermission(new PropertyPermission("user.timezone", "write"));
   } catch (SecurityException e) {
    hasPermission = false;
   }
  }
  return hasPermission;
 }

 /**
  * Sets the <code>TimeZone</code> that is
  * returned by the <code>getDefault</code> method.  If <code>zone</code>
  * is null, reset the default to the value it had originally when the
  * VM first started.
  * @param zone the new default time zone
  * @see #getDefault
  */
 public static void setDefault(TimeZone zone)
 {
  if (hasPermission()) {
   synchronized (TimeZone.class) {
    defaultTimeZone = zone;
    defaultZoneTL.set(null);
   }
  } else {
   defaultZoneTL.set(zone);
  }
 }

而之前(在JDK5中)它只是:

while before (in JDK5) it was simply:

 /**
  * Sets the <code>TimeZone</code> that is
  * returned by the <code>getDefault</code> method.  If <code>zone</code>
  * is null, reset the default to the value it had originally when the
  * VM first started.
  * @param zone the new default time zone
  * @see #getDefault
  */
 public static synchronized void setDefault(TimeZone zone)
 {
  defaultZoneTL.set(zone);
 }


推荐答案

实际上搜索bug数据库非常好的主意:)

Searching the bugs database was actually quite a good idea :)

http://bugs.sun.com/view_bug.do?bug_id=6352812

以及(re docs):

and also (re docs):

http://bugs.sun.com/view_bug.do ?bug_id = 6181786

摘要:JDK 1.5是规则的一个例外,JDK 1.6的东西恢复到'正常',根据docs,是时区更改是VM范围。

Summary: JDK 1.5 was an exception to the rule, with JDK 1.6 things are back to 'normal', which, according to the docs, is that a timezone change is VM wide.

这篇关于JDZ6中的TimeZone.setDefault更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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