javacalendarical4j

ical4j - Find Event based on the UID


I´m using ical4j to create my .ical - File and save events. But how can I find a stored VEvent in the calendar?

I have the following code, but it´s not working? I got my calendar -> this is working and I debug this already

public VEvent findEvent(CalendarExtern calendarExtern, String hashId) throws IOException, ParserException {

    Calendar calendar = readCalenderFromFile(calendarExtern);

    for (Component component : calendar.getComponents(Component.VEVENT)) {
          if (hashId.equals(component.getProperty(Property.UID))) {
              VEvent event = (VEvent) component;
                return event;
          }
        } 

    return null;
}

Any ideas? Many thanks


Solution

  • component.getProperty(Property.UID) returns a Property so what you really want to do is:

    if (hashId.equals(component.getProperty(Property.UID).getValue()))...
    

    Of course you, depending on your input, you may want to check for components without a UID property before doing getValue().