How to parse an iCal RRULE in Java

China☆狼群 提交于 2019-12-01 08:54:30

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

The solution is to use:

        <dependency>
            <groupId>org.scala-saddle</groupId>
            <artifactId>google-rfc-2445</artifactId>
            <version>20110304</version>
        </dependency>

Few examples:

1 convert to java object:

rule = new RRule("RRULE:FREQ=WEEKLY;INTERVAL=2;BYDAY=TU,WE,TH");

2 convert back:

rule.toIcal();

Here is how I have generated a couple of (dateStart - dateEnd) with https://github.com/mangstadt/biweekly library.

The incomings are: - a start date - a end date (facultatif) - a RRULE, separated by |

DTSTART:2019-07-01T16:00:00Z|DTEND:2019-07-01T17:00:00Z|RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR,SA,SU;UNTIL=2019-07-31T23:59:59Z

Code:

// A.  split filter $iCalFilter
Map<String, String> iCalFilterMap = new HashMap<>();
for (String part : iCalFilter.split("\\|")) {
    if (part.contains(":")) {
        String[] subparts = part.split(":", 2);
        iCalFilterMap.put(subparts[0], subparts[1]);
    }
}

// B. generate couples of dates starts and  dates ends
ParseContext context = new ParseContext();
context.setVersion(ICalVersion.V2_0);
RecurrenceRuleScribe scribe = new RecurrenceRuleScribe();
RecurrenceRule rrule = scribe.parseText(iCalFilterMap.get(iCalFilterConstant.RRULE), ICalDataType.DATE_TIME, new ICalParameters(), context);
TimeZone timezone = TimeZone.getTimeZone(iCalFilterConstant.UTC);
int iCalFilterMaximumDayToGenerate = 365;

        // DTSTART && DTEND
        List<DateTime> listeDateStart = new ArrayList<>();
        List<DateTime> listeDateEnd = new ArrayList<>();

        if (null != inCalendarMap.get(InCalendarConstant.DTSTART)) {
            DateTime dateTimeStart = new DateTime(inCalendarMap.get(InCalendarConstant.DTSTART)).withZone(InCalendarConstant.DATE_TIME_ZONE_UTC);

            DateTime dateTimeEnd;
            if (null != inCalendarMap.get(InCalendarConstant.DTEND)) {
                dateTimeEnd = new DateTime(inCalendarMap.get(InCalendarConstant.DTEND)).withZone(InCalendarConstant.DATE_TIME_ZONE_UTC);
            } else {
                // Si le DTEND n'est pas renseigné par le client, alors on le met par défaut en fin de journée
                dateTimeEnd = new DateTime(inCalendarMap.get(InCalendarConstant.DTSTART)).withZone(InCalendarConstant.DATE_TIME_ZONE_UTC).withTime(23, 59, 59, 999);
            }

            Interval interval = new Interval(dateTimeStart, dateTimeEnd);

            DateIterator ditStart = rrule.getDateIterator(dateTimeStart.toDate(), timezone);
            int compteurDateStart = 0;
            while (ditStart.hasNext()) {
                DateTime dateStart = new DateTime(ditStart.next()).withZone(InCalendarConstant.DATE_TIME_ZONE_UTC);
                listeDateStart.add(dateStart);

                DateTime dateEnd = dateStart.plus(interval.toDurationMillis());
                listeDateEnd.add(new DateTime(dateEnd).withZone(InCalendarConstant.DATE_TIME_ZONE_UTC));
                logger.logDebug(dateStart + "---" + dateEnd);

                // Pour éviter une boucle infinie, ou une date de génération en 9999-12-12T23:59:59 , on limite le nombre de jours
                compteurDateStart++;
                if(compteurDateStart > inCalendarMaximumDayToGenerate) {
                    break;
                }
            }
        }

        // C. on set le couple dans le filters LesHalles pour utilisation dans la requete bdd voir CourseJdbcDaoImpl.addInCalendar
        if(listeDateStart.size() == 0 || listeDateEnd.size() == 0) {
            throw new ServiceFunctionalException(new Object[] { inCalendar  }, MessageFonctionnelTheorique.IN_CALENDAR_RRULE_AUCUNE_DATE_GENEREE);
        }
        ImmutablePair<List<DateTime>, List<DateTime>> pairListeDatesStartsDatesEnds = new ImmutablePair<>(listeDateStart, listeDateEnd);
        filters.setInCalendarDateStartDateEnds(pairListeDatesStartsDatesEnds);
    }

You can now use this couple of datestart dateends for your database query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!