2010-09-11 43 views
2

首先,對不起,這是如此之久。我可能不需要所有的代碼,但想確定。StackOverflowError使用joda時間新時期(長)

其次,我的實際問題是,我做錯了什麼,或者這是一個在喬達時間庫中的錯誤?

我想使用喬達時間(1.6.1)來計算,然後格式化持續時間。

我目前使用Period,這可能是錯誤的選擇。請讓我知道是否。 但是,即使這是錯誤的選擇,我敢肯定,這不應該發生。

我使用毫秒初始化一個Period(通過將持續時間乘以1000秒)。我使用的是Period,所以我可以然後格式化並打印:

long durationLong = durationSec * 1000; 
Period duration = new Period(durationLong); 

PeriodFormatter daysHoursMinutes = new PeriodFormatterBuilder() 
    .appendHours() 
    .appendSeparator(":") 
    .appendMinutes() 
    .appendSeparator(":") 
    .appendSeconds() 
    .toFormatter(); 

String formattedString = daysHoursMinutes.print(callDuration.normalizedStandard()); 

我得到下面的異常,並通過源已經看過確認循環。

Caused by: java.lang.StackOverflowError 
    at java.util.Hashtable.get(Hashtable.java:274) 
    at java.util.Properties.getProperty(Properties.java:177) 
    at java.lang.System.getProperty(System.java:440) 
    at java.lang.System.getProperty(System.java:412) 
    at org.joda.time.DateTimeZone.getDefault(DateTimeZone.java:132) 
    at org.joda.time.DateTimeZone.forID(DateTimeZone.java:190) 
    at org.joda.time.DateTimeZone.getDefault(DateTimeZone.java:132) 
    at org.joda.time.DateTimeZone.forID(DateTimeZone.java:190) 

...snip (all the same)... 

    at org.joda.time.DateTimeZone.getDefault(DateTimeZone.java:132) 
    at org.joda.time.DateTimeZone.forID(DateTimeZone.java:190) 
    at org.joda.time.DateTimeZone.getDefault(DateTimeZone.java:132) 
    at org.joda.time.DateTimeZone.forID(Dat 

週期(長):

public Period(long duration) { 
    super(duration, null, null); 
} 

超級(長,PeriodType,年表):

protected BasePeriod(long duration, PeriodType type, Chronology chrono) { 
    super(); 
    type = checkPeriodType(type); 
    chrono = DateTimeUtils.getChronology(chrono); 
    iType = type; 
    iValues = chrono.get(this, duration); 
} 

DateTimeUtils.getChronology(計時):

public static final Chronology getChronology(Chronology chrono) { 
    if (chrono == null) { 
     return ISOChronology.getInstance(); 
    } 
    return chrono; 
} 

ISOChronology .getInstance():

public static ISOChronology getInstance() { 
    return getInstance(DateTimeZone.getDefault()); 
} 

DateTimeZone.getDefault():

public static DateTimeZone getDefault() { 
    DateTimeZone zone = cDefault; 
    if (zone == null) { 
     synchronized(DateTimeZone.class) { 
      zone = cDefault; 
      if (zone == null) { 
       DateTimeZone temp = null; 
       try { 
        try { 
         temp = forID(System.getProperty("user.timezone")); 
        } catch (RuntimeException ex) { 
         // ignored 
        } 
        if (temp == null) { 
         temp = forTimeZone(TimeZone.getDefault()); 
        } 
       } catch (IllegalArgumentException ex) { 
        // ignored 
       } 
       if (temp == null) { 
        temp = UTC; 
       } 
       cDefault = zone = temp; 
      } 
     } 
    } 
    return zone; 
} 

FORID(字符串)調用getDefault(),它創建循環:

public static DateTimeZone forID(String id) { 
    if (id == null) { 
     return getDefault(); 
    } 
    if (id.equals("UTC")) { 
     return DateTimeZone.UTC; 
    } 
    DateTimeZone zone = cProvider.getZone(id); 
    if (zone != null) { 
     return zone; 
    } 
    if (id.startsWith("+") || id.startsWith("-")) { 
     int offset = parseOffset(id); 
     if (offset == 0L) { 
      return DateTimeZone.UTC; 
     } else { 
      id = printOffset(offset); 
      return fixedOffsetZone(id, offset); 
     } 
    } 
    throw new IllegalArgumentException("The datetime zone id is not recognised: " + id); 
} 

回答

5

作爲循環部分只在約達代碼,我會說這是一個錯誤。


它已在trunk上更正並將在V2.0中提供。


資源:

+0

給你鏈接到主幹和錯誤報告的答案 - 我找不到! – cofiem 2010-09-11 13:47:44

+0

請參閱版本v1.6.2 – JodaStephen 2010-09-13 23:41:26

3

看起來像它的,因爲它假定user.timezone屬性將被設置的錯誤。

這是在這種情況下得到它的方式 - 只要確保user.timezone設置得當。這是一個恥辱,你必須儘管。

Joda Time在很多地方使用「null表示默認」 - 不幸的是,在我看來。我通常更喜歡「空無效」。在Noda Time(Joda Time to .NET的一個端口)中,我們試圖擺脫很多這種事情 - 以及防止默認時區在第一時間流行。

+0

+1瞭解如何避開它。 – cofiem 2010-09-11 13:47:07