简体   繁体   English

JDZ6中的TimeZone.setDefault更改

[英]TimeZone.setDefault changes in JDK6

I just noticed that JDK 6 has a different approach to setting a default TimeZone than JDK5. 我刚刚注意到JDK 6设置默认TimeZone的方法与JDK5不同。

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! 使用JDK6(我刚刚查看了1.6.0.18),实现已经改变,因此如果用户可以写入“user.timezone”属性,或者如果没有安装SecurityManager,则时区会在整个VM范围内发生变化! 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. 这似乎是一个相当大的变化,我在网上找不到任何关于它的东西。

Here is the JDK6 code: 这是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);
  }
 }

while before (in JDK5) it was simply: 之前(在JDK5中)它只是:

 /**
  * 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);
 }

Searching the bugs database was actually quite a good idea :) 搜索bug数据库实际上是个不错的主意:)

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

and also (re docs): 还有(重新说明):

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

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. 简介:JDK 1.5是规则的一个例外,JDK 1.6的东西恢复到“正常”,根据文档的说法,时区变化是VM范围的。

This was probably done to fix a bug. 这可能是为了解决一个错误。 I'd search bugs.sun.com to find the rationale for it. 我会搜索bugs.sun.com找到它的基本原理。 (Clues might also be found in the release notes .) (也可以在发行说明中找到线索。)

The API documentation for TimeZone.getDefault() states that "the source of the default TimeZone may vary with implementation." TimeZone.getDefault()的API文档声明“默认TimeZone的来源可能因实现而异。” If your code relies on implementation specific behaviour of the standard API classes (in this case, that the default time zone is kept at a thread local level), you must expect that your code fails with newer versions of the VM or with VMs from different vendors. 如果您的代码依赖于标准API类的特定于实现的行为(在这种情况下,默认时区保留在线程本地级别),则必须预期您的代码会因较新版本的VM或来自不同VM的VM而失败供应商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM