RFC5545。同时计算RRULE和EXDATE(EXRULE)的事件发生次数

时间:2017-07-14 11:00:18

标签: java calendar find-occurrences rfc5545 ical4j

我必须计算事件发生次数。计算应基于重复事件模式。循环事件模式基于rfc5545

我找到lib-recur库来计算事件。在描述之后,我看到他们使用特定实例集的实例(例如,rrule,exrule,rdates或exdates列表)提供计算。我用RRULE计算,看起来很有效。我需要在我的逻辑中正确找到所有事件,包括计算 EXDATE EXRULE 。但似乎最后排除在这里不支持。 小代码示例:

import org.dmfs.rfc5545.DateTime;
import org.dmfs.rfc5545.recurrenceset.RecurrenceRuleAdapter;
import org.dmfs.rfc5545.recurrenceset.RecurrenceSet;
import org.dmfs.rfc5545.recurrenceset.RecurrenceSetIterator;
@Test
public void testSpecial() throws InvalidRecurrenceRuleException {
    RecurrenceRule recurrenceRule = new RecurrenceRule("FREQ=YEARLY;BYMONTHDAY=23;BYMONTH=5;COUNT=3");
    RecurrenceRuleIterator it = recurrenceRule.iterator(DateTime.nowAndHere());
    int maxInstances = 10; // limit instances for rules that recur forever
    while (it.hasNext() && (!recurrenceRule.isInfinite() || maxInstances-- > 0)) {
        DateTime nextInstance = it.nextDateTime();
        // do something with nextInstance
        System.out.println(nextInstance);
    }
}

结果:

  

20170714T163325

     

20180523T163325

     

20190523T163325

我也知道google based library可以为RRULES提供核心处理程序,EXRULE ...基于rfc5545标准。但是我还没有找到如何在这里计算出现次数(包括RRULES,EXDATE,EXRULE)。

更多示例,但适用于google-rfc-2445库:

import com.google.ical.compat.jodatime.LocalDateIterable;
import com.google.ical.compat.jodatime.LocalDateIterator;
import com.google.ical.compat.jodatime.LocalDateIteratorFactory;
import com.google.ical.values.RRule;
@Test
public void test() throws ParseException {
    String sRule = "RRULE:FREQ=YEARLY;COUNT=3;INTERVAL=2;BYMONTH=5;BYMONTHDAY=22,23,24,25,26,27,28;BYDAY=MO";
    LocalDateIterable localDateIterable = LocalDateIteratorFactory
            .createLocalDateIterable(sRule, org.joda.time.LocalDate.now(), true);
    LocalDateIterator iterator = localDateIterable.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }
}

结果:

  

2017年7月14日

     

2019年5月27日

     

2021年5月24日

     

2023年5月22日

请分享已经具有复杂事件计算功能的开源库的链接(计算应同时包括RRULE,EXDATE)。

1 个答案:

答案 0 :(得分:2)

lib-recur支持计算重复集。虽然,这个组件的界面还不完善(并不是很方便)。

以下是一个简单的示例,说明它如何与一个RRULE和一个EXRULE一起使用:

// note: recurrence expansion takes place in a specific time zone
TimeZone tz = TimeZone.getDefault();
// the start of the recurring event
DateTime start = new DateTime(tz, 2017, 5 - 1 /* zero based */, 23, 12, 0, 0);

// parse the RRULE
RecurrenceRule recurrenceRule = new RecurrenceRule("FREQ=YEARLY;BYMONTHDAY=23;BYMONTH=5;COUNT=30");
// parse the EXRULE (in this case every 3rd instance of the RRULE)
RecurrenceRule exceptionRule = new RecurrenceRule("FREQ=YEARLY;INTERVAL=3;BYMONTHDAY=23;BYMONTH=5;COUNT=10");

// create an empty RecurrenceSet
// a recurrence set is the set of all actual instance of a recurring event and may consist of multiple instance and excludes sources 
RecurrenceSet recurrenceSet = new RecurrenceSet();
// add the instances of the RRULE
recurrenceSet.addInstances(new RecurrenceRuleAdapter(recurrenceRule));
// exclude the instances of the EXRULE
recurrenceSet.addExceptions(new RecurrenceRuleAdapter(exceptionRule));

RecurrenceSetIterator recurrenceSetIterator = recurrenceSet.iterator(start.getTimeZone(), start.getTimestamp());

int maxInstances = 10; // limit instances for rules that recur forever
// iterate as long as the recurrence set still has instances
while (recurrenceSetIterator.hasNext() && (!recurrenceRule.isInfinite() || maxInstances-- > 0))
{
    // get the next instance of the recurrence set
    DateTime nextInstance = new DateTime(start.getTimeZone(), recurrenceSetIterator.next());
    // do something with nextInstance
    System.out.println(nextInstance);
}

您可以看到RecurrenceSetlong个时间戳上运行,这远非理想。

要支持RDATESEXDATES,您可以使用RecurrenceList适配器。 RecurrenceList要么采用一组时间戳,要么是一个时区和一串逗号分隔的日期时间或日期值(就像它们是为RDATES和EXDATES定义的那样):

// add more RDATES instances 
recurrenceSet.addInstances(new RecurrenceList("20171212T121212,20181212T121212", tz));

// add EXDATES
recurrenceSet.addExceptions(new RecurrenceList("20180523T120000,20190523T120000", tz));

注意,与RecurrenceRuleIterator相比,此组件还没有任何测试覆盖率。