我的程序暂停并打印出此错误:
org.joda.time.IllegalFieldValueException: Value 366 for dayOfYear must be in the range [1,365]
at org.joda.time.field.FieldUtils.verifyValueBounds(FieldUtils.java:217)
at org.joda.time.field.PreciseDurationDateTimeField.set(PreciseDurationDateTimeField.java:78)
at org.joda.time.chrono.ZonedChronology$ZonedDateTimeField.set(ZonedChronology.java:466)
at org.joda.time.DateTime.withDayOfYear(DateTime.java:1716)
日期是
2013-12-31T23:59:52.000Z
一年中的某一天应该是365.为什么Joda返回366然后抛出错误?
修改
错误的其余部分只包含有关我班级的信息:
at MainProgram.processMinuteLevelAGCRecords(MainProgram.java:405)
at MainProgram.createMinuteLevelAGCFile(MainProgram.java:274)
at MainProgram.main(MainProgram.java:106)
试图获取一年中某一天的代码看起来像这样,因为我需要格式为“yyDDD”的朱利安日期:
Integer.parseInt(String.format("%04d", date.getYear()).substring(2)
+ String.format("%03d", date.getDayOfYear()));
失败时的“日期”是:
2013-12-31T23:59:52.000Z
第405行
DateTime dt = new DateTime(0).withYear(year).withDayOfYear(day).withHourOfDay(0).withMinuteOfHour(0).withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")));
编辑2
我有一个从数据库中检索的时间戳派生的DateTime对象。我的DateTime对象称为“日期”。当我打印出“日期”时,我得到了
date = 2013-12-31T23:59:52.000Z
现在,使用上面的DateTime对象“date”的代码更多:
int julianDay = Integer.parseInt(String.format("%04d", date.getYear()).substring(2)
+ String.format("%03d", date.getDayOfYear()));
int year = julianDay/1000;
year = Integer.parseInt(startDate.substring(0,2) + Integer.toString(year));
int day = Integer.parseInt(Integer.toString(julianDay).substring(2));
DateTime dt = new DateTime(0).withYear(year).withDayOfYear(day).withHourOfDay(0).withMinuteOfHour(0).withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")));
由于第366天,它在上一行失败了。
答案 0 :(得分:2)
问题显然不在Joda-Time:
public static void main(String[] args)
{
DateTime d = new DateTime(2013,12,31,23,59,52,DateTimeZone.UTC);
System.out.println(d);
System.out.println(d.getDayOfYear());
}
打印
2013-12-31T23:59:52.000Z
365
我建议您使用IDE调试器单步执行代码。
(我知道这可能更多的是评论而不是答案,但很难将可读代码放在评论中)
答案 1 :(得分:0)
int julianDay = ((date.getYear() % 100) * 1000) + date.getDayOfYear(); // If you really need this
DateTime dt = new DateTime(0).withYear(date.getYear()).withDayOfYear(date.getDayOfYear()).withHourOfDay(0).withMinuteOfHour(0).withZoneRetainFields(DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC")));
(不确定最后的所有时区内容 - Joda Time不是我的事。)