Search code examples
javaicalendar

Evaluate if iCal `event` is currently in progress


Im currently using a library called biweekly to parse iCal events in java. I have no issue swapping libraries if a better solution is present so all java related solutions would be welcome.

Im trying to create a function that checks an iCal schedule to work out if any events are currently in progress / active.

Biweekly helped cleanly parse the iCal schedule events like this:


List<ICalendar> icals = Biweekly.parse(schedule).all();

for (ICalendar ical : icals) {

    for (VEvent event : ical.getEvents()) {

        // I need to compute if the event is currently active

    }
}

We found an example that helps get the events as a DateIterator but we can't as yet connect that to knowing if an event is currently active.


TimezoneInfo tzInfo = ical.getTimezoneInfo();

DateStart dateStart = event.getDateStart();

TimeZone timezone;
if (tzInfo.isFloating(dateStart)){
    timezone = TimeZone.getDefault();
} else {
    TimezoneAssignment timezoneAssignment = tzInfo.getTimezone(dateStart);
    timezone = (timezoneAssignment == null) ? TimeZone.getTimeZone("UTC") : 
    timezoneAssignment.getTimeZone();
}

DateIterator it = event.getDateIterator(timezone);


Solution

  • As discussed in comments, it seems the method you're looking for is getStatus() of the class VEvent:

    /**
     * Gets the status of the event.
     * @return the status or null if not set
     * @see <a href="http://tools.ietf.org/html/rfc5545#page-92">RFC 5545
     * p.92-3</a>
     * @see <a href="http://tools.ietf.org/html/rfc2445#page-88">RFC 2445
     * p.88-9</a>
     * @see <a href="http://www.imc.org/pdi/vcal-10.doc">vCal 1.0 p.35-6</a>
     */
    public Status getStatus() {
        return getProperty(Status.class);
    }
    

    GitHub documentation: https://github.com/mangstadt/biweekly/blob/master/src/main/java/biweekly/component/VEvent.java#L501.

    Be careful when you handle this value, according to the documentation it may be null so always null-check before doing whatever you think to do.