javaical4j

How to create recurring event .ics file using ical4j in java?


This is the sample code for creating .ics file using java for normal event.

    Calendar calendar = new Calendar();
    calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
    calendar.getProperties().add(Version.VERSION_2_0);
    calendar.getProperties().add(CalScale.GREGORIAN);

    java.util.Calendar cal = java.util.Calendar.getInstance();
    cal.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
    cal.set(java.util.Calendar.DAY_OF_MONTH, 25);
    // initialise as an all-day event..
    VEvent christmas = new VEvent(new Date(cal.getTime()), "Christmas Day");
    // Generate a UID for the event..
    UidGenerator ug = new UidGenerator("1");
    christmas.getProperties().add(ug.generateUid());
    calendar.getComponents().add(christmas);

    ServletOutputStream fout = response.getOutputStream();
    CalendarOutputter outputter = new CalendarOutputter();
    outputter.output(calendar, fout);

Solution

  • Lets say your recurrence pattern is

    String recurrencePattern = "FREQ=MONTHLY;INTERVAL=1;BYDAY=SU;BYSETPOS=1;COUNT=5"
    

    then you can add recurrence information in your current event in this way.

    VEvent christmas = new VEvent(new Date(cal.getTime()), "Christmas Day");
    christmas.getProperties().add(new RRule(recurrencePattern));