2010-02-01 5 views
16

JDK 6에는 JDK5보다 기본 TimeZone을 설정하는 방법이 다릅니다.TimeZone.setDefault가 JDK6에서 변경되었습니다.

이전에는 새 기본값이 스레드 로컬 변수에 저장되었습니다. JDK6 (1.6.0.18)에서 구현이 변경되었으므로 사용자가 "user.timezone"속성에 쓸 수 있거나 SecurityManager가 설치되어 있지 않으면 VM의 시간대가 변경됩니다! 그렇지 않으면 스레드 로컬 변경이 발생합니다.

내가 틀렸어? 이것은 상당히 과감한 변화 인 것으로 보이며, 웹에서 그걸 발견 할 수 없었습니다.

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에) 단순히했다 : 이것은 아마 버그를 해결하기 위해 이루어졌다

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

답변

4

다음은 JDK6 코드입니다. 나는 그것에 대한 근거를 찾기 위해 bugs.sun.com을 찾을 것입니다. (단서도 release notes에서 찾을 수 있습니다.)

1

에는 TimeZone.getDefault (대한 API 문서가)한다고 "기본의 소스가 시간대가 구현과 다를 수 있습니다." 코드가 표준 API 클래스의 구현 특정 동작 (이 경우 기본 표준 시간대는 스레드 로컬 수준에서 유지됨)에 의존하는 경우 새 버전의 VM 또는 다른 VM의 코드에서 코드가 실패 할 것으로 예상해야합니다 공급 업체. 버그 데이터베이스를 검색