代码之家  ›  专栏  ›  技术社区  ›  cofiem

StackOverflowerr使用joda时间新时段(长)

  •  2
  • cofiem  · 技术社区  · 14 年前

    首先,对不起,时间太长了。我可能不需要所有的代码,但我想确定一下。

    第二,我真正的问题是,我是做错了什么,还是这是joda时间库中的一个bug?

    我尝试使用joda time(1.6.1)来计算,然后格式化持续时间。

    Period ,这可能是错误的选择。如果是,请告诉我。 然而,即使这是一个错误的选择,我很肯定这不应该发生。

    我正在初始化一个 期间 使用毫秒(将持续时间(秒)乘以1000)。我用的是 然后我可以格式化并打印:

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

    超级(长,周期类型,年表):

    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;
    }
    

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

    日期时区.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(String)调用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);
    }
    
    2 回复  |  直到 14 年前
        1
  •  5
  •   Colin Hebert    14 年前


    它已经被修正了 trunk


        2
  •  3
  •   Jon Skeet    14 年前

    看起来这是一个错误,因为它假设 user.timezone 属性将被设置。

    Joda Time在很多地方使用“null表示默认值”——不幸的是,在我看来。我通常更喜欢“空是无效的”。在 Noda Time (Joda Time to.NET的一个端口)我们正在尝试摆脱很多这样的事情——同时也在一开始就防止默认时区如此流行。