2010-09-11 41 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