首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >JDK6中的TimeZone.setDefault更改

JDK6中的TimeZone.setDefault更改
EN

Stack Overflow用户
提问于 2010-02-01 21:03:31
回答 2查看 9.8K关注 0票数 17

我刚刚注意到,JDK6设置默认TimeZone的方法与JDK5不同。

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

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

下面是JDK6代码:

代码语言:javascript
复制
 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中),它很简单:

代码语言:javascript
复制
 /**
  * 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);
 }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-02-01 22:01:21

搜索bug数据库实际上是一个很好的想法:)

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

还有():

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

摘要: JDK 1.5是规则的一个例外,JDK 1.6的事情回到了“正常”,根据文档,这是一个时区更改是VM范围的。

票数 13
EN

Stack Overflow用户

发布于 2010-02-01 21:46:14

TimeZone.getDefault()的应用程序接口文档指出“默认TimeZone的源代码可能因实现而异”。如果您的代码依赖于标准API类的特定于实现的行为(在这种情况下,默认时区保持在线程本地级别),那么您必须预料到,使用较新版本的VM或来自不同供应商的VM时,您的代码将失败。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2176784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档