Search code examples
javaicalendarrrule

How to parse an iCal RRULE in Java


I have the following iCal recurrence rule examples:

"RRULE:FREQ=YEARLY;INTERVAL=2"
"RRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,WE,TH"

I need a Java library to parse the RRULE patterns to handle in an object. Are there any good Java libraries?



Solution

  • You can use lib-recur

    It is still supported and handle RFC 5545 and RFC 2445.

    RecurrenceRule rule = new RecurrenceRule("FREQ=YEARLY;BYMONTHDAY=23;BYMONTH=5");
    
    DateTime start = new DateTime(1982, 4 /* 0-based month numbers! */,23);
    
    RecurrenceRuleIterator it = rule.iterator(start);
    
    int maxInstances = 100; // limit instances for rules that recur forever
    
    while (it.hasNext() && (!rule.isInfinite() || maxInstances-- > 0))
    {
        DateTime nextInstance = it.nextDateTime();
        // do something with nextInstance
    }
    

    You can install it with maven

    <!-- https://mvnrepository.com/artifact/org.dmfs/lib-recur -->
    <dependency>
        <groupId>org.dmfs</groupId>
        <artifactId>lib-recur</artifactId>
         <version>0.10.2</version>
    </dependency>
    

    Or with gradle

    // https://mvnrepository.com/artifact/org.dmfs/lib-recur 
    compile group: 'org.dmfs', name: 'lib-recur', version: '0.10.2'
    

    More documentation is available here : https://github.com/dmfs/lib-recur