【发布时间】:2014-05-30 13:06:26
【问题描述】:
我需要在 2 月的最后一天重复一个日期,我正在使用 Joda 时间来使用规则来生成几天、几周、几个月等的日期。因为 Months 工作正常,但大约几年时我收到错误输出。
private static List<DateTime> calculateRecurrentDates(DateTime startDate, String recurrentRule) {
List<DateTime> dates = new ArrayList<DateTime>();
TimeStamp startDate = Timestamp.valueOf("2011-02-28 10:10:10");
String recurrentRule = "RRULE:FREQ=YEARLY;COUNT=6;INTERVAL=1;";
String leapYearRule = "RRULE:FREQ=YEARLY;COUNT=6;INTERVAL=1;BYMONTHDAY=28,29;BYSETPOS=-1";
try {
DateTimeIterable range = DateTimeIteratorFactory.createDateTimeIterable(recurrentRule, startDate, DateTimeZone.UTC, true);
for (DateTime date : range) {
dates.add(date);
}
} catch (ParseException e) {
dates = null;
logger.error(e.getMessage());
}
return dates;
}
但我收到了这个:
2011-02-28T10:10:10.000Z
2012-02-28T10:10:10.000Z
2013-02-28T10:10:10.000Z
2014-02-28T10:10:10.000Z
2015-02-28T10:10:10.000Z
2016-02-28T10:10:10.000Z
如果是闰年的话:
2012-02-29T10:10:10.000Z
2012-12-29T10:10:10.000Z
2013-12-29T10:10:10.000Z
2014-12-29T10:10:10.000Z
2015-12-29T10:10:10.000Z
2016-12-29T10:10:10.000Z
2017-12-29T10:10:10.000Z
我如何编写一条规则来获得每年二月的最后一天?
【问题讨论】:
-
找到 3 月 1 日,然后减去 1 天是我这样做的方式。
-
你真的需要一个带时间的日期还是一个简单的日期?如果是后者,永远不要使用 DateTime,使用 LocalDate。
标签: java jodatime icalendar rfc2445