使用 joda-time new period(long) 时出现 StackOverflowError
首先,抱歉这么长。我可能不需要所有代码,但想确定一下。
其次,我的实际问题是,我是否做错了什么,或者这是 joda-time 库中的错误?
我正在尝试使用 joda-time (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
period(long):
public Period(long duration) {
super(duration, null, null);
}
super(long, periodType, Chronology):
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(chrono):
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(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);
}
First, sorry this is so long. I probably don't need all the code, but wanted to be sure.
Second, my actual question is, am I doing something wrong, or is this a bug in the joda-time library?
I'm trying to use joda-time (1.6.1) to calculate, then format time durations.
I'm currently using Period
, which may be the wrong choice. Please let me know if it is.
However, even if it is the wrong choice, I'm pretty sure this shouldn't happening.
I'm initialising a Period
using milliseconds (by multiplying a duration in seconds by 1000). I'm using the Period
so I can then format it and print it:
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());
I get the Exception below, and have looked through the source to confirm the loop.
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
Period(long):
public Period(long duration) {
super(duration, null, null);
}
super(long, PeriodType, Chronology):
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(chrono):
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(String) calls getDefault(), which creates the loop:
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于循环部分仅在 joda 代码中,我认为这是一个错误。
它已在 trunk 并将在 V2.0 中提供。
资源:
As the looping part is only in the joda code, I would say that's a bug.
It has been corrected on the trunk and will be available in V2.0.
Resources :
看起来这是一个错误,因为它假设已经设置了 user.timezone 属性。
在这种情况下,这就是解决这个问题的方法 - 只需确保 user.timezone 设置正确即可。遗憾的是你不得不这么做。
Joda Time 在很多地方使用“null 表示默认”——不幸的是,在我看来。我通常更喜欢“null 无效”。在 Noda Time(Joda Time 到 .NET 的端口)中,我们试图摆脱很多这样的事情 - 以及从一开始就防止默认时区如此普遍。
Looks like it's a bug in that it assumes the
user.timezone
property will have been set.That's the way to get round it in this case - just make sure that user.timezone is set appropriately. It's a shame that you have to though.
Joda Time uses "null means default" in a lot of places - unfortunately, in my view. I prefer "null is invalid" usually. In Noda Time (a port of Joda Time to .NET) we're trying to get rid of a lot of this kind of thing - as well as preventing the default time zone from being as prevalent in the first place.