Add multiple events to ical with biweekly

1.8k Views Asked by At

I have the following method that uses biweekly to create and ics string:

public String createICalendarString(List<ICalendarEventVo> iCalendarEventVos, String locale, String timeZoneCanonicalId) throws IOException
{
    String icsLocale = convertLocaleToIcsFormat(locale);
    ICalendar ical = new ICalendar();
    for (ICalendarEventVo iCalendarEventVo : iCalendarEventVos)
    {
        VEvent event = new VEvent();
        Summary summary = new Summary(iCalendarEventVo.getSummaryText());
        summary.setLanguage(icsLocale);
        event.setSummary(summary);

        if (null != iCalendarEventVo.getDescriptionText())
        {
            Description description = new Description(iCalendarEventVo.getDescriptionText());
            description.setLanguage(icsLocale);
            event.setDescription(description);
        }

        if (null != iCalendarEventVo.getLocationText())
        {
            Location location = new Location(iCalendarEventVo.getLocationText());
            location.setLanguage(icsLocale);
            event.setLocation(location);
        }

        if (null != iCalendarEventVo.getOrganizerName() && null != iCalendarEventVo.getOrganizerEmail())
        {
            Organizer organizer = new Organizer(iCalendarEventVo.getOrganizerName(), iCalendarEventVo.getOrganizerEmail());
            event.setOrganizer(organizer);
        }

        event.setDateStart(iCalendarEventVo.getStartDate());
        event.setDateEnd(iCalendarEventVo.getEndDate());
        event.setUid(iCalendarEventVo.getUid());

        ical.addEvent(event);
    }

    String icsString = null;

    try (StringWriter writer = new StringWriter();
            ICalWriter icalWriter = new ICalWriter(writer, ICalVersion.V2_0);)
    {
        //optional: Use Outlook-friendly VTIMEZONE components:
        icalWriter.getTimezoneInfo().setGenerator(new TzUrlDotOrgGenerator(true));

        //output date/time values in the timeZone that was passed in
        TimeZone timeZone = TimeZone.getTimeZone(timeZoneCanonicalId);
        icalWriter.getTimezoneInfo().setDefaultTimeZone(timeZone);

        icalWriter.write(ical);
        icsString = writer.toString();
    }
    return icsString;
}

Which generates the following:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.4.4//EN
BEGIN:VEVENT
DTSTAMP:20151128T030141Z
SUMMARY;LANGUAGE=en-us:Test ILT Class: Session #1
DESCRIPTION;LANGUAGE=en-us:Test ILT Class goes through what is needed to ma
 ke a sale. The session will be about looking nice for your customer.
LOCATION;LANGUAGE=en-us:Statue of Liberty - Meet at the top of the crown.
ORGANIZER;CN=TorchLMS:mailto:[email protected]
DTSTART;TZID=America/New_York:20151201T190000
DTEND;TZID=America/New_York:20151201T220000
UID:TLMS_SESSION_1000110010000000000_1000110210000000051
END:VEVENT
BEGIN:VEVENT
DTSTAMP:20151128T030141Z
SUMMARY;LANGUAGE=en-us:Test ILT Class: Session #2
DESCRIPTION;LANGUAGE=en-us:Test ILT Class goes through what is needed to ma
 ke a sale. This session will talk about how to blow sunshine in the direct
 ion of the customer.
LOCATION;LANGUAGE=en-us:World Trade Center - Top floor in the penthouse
ORGANIZER;CN=TorchLMS:mailto:[email protected]
DTSTART;TZID=America/New_York:20151202T170000
DTEND;TZID=America/New_York:20151202T190000
UID:TLMS_SESSION_1000110010000000000_1000110210000000101
END:VEVENT
BEGIN:VTIMEZONE
TZID:America/New_York
TZURL:http://tzurl.org/zoneinfo-outlook/America/New_York
X-LIC-LOCATION:America/New_York
BEGIN:DAYLIGHT
TZOFFSETFROM:-0500
TZOFFSETTO:-0400
TZNAME:EDT
DTSTART:19700308T020000
RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3
END:DAYLIGHT
BEGIN:STANDARD
TZOFFSETFROM:-0400
TZOFFSETTO:-0500
TZNAME:EST
DTSTART:19701101T020000
RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11
END:STANDARD
END:VTIMEZONE
END:VCALENDAR

The problem is when I email this to my gmail account only the first event shows up. At first I thought this was an issue with Biweekly, but it looks like the ics out put is correct. Is there something wrong with the ics output?

2

There are 2 best solutions below

1
On

Hi tad: It worked for me. Here is how I did it:

  1. Click on the little down arrow to the right of "My calendars" and select "Settings".
  2. Click on "Import Calendar" (next to the "Create calendar" button).
  3. Select the .ics file in the file chooser.
  4. Note the name of the calendar that's selected in the "Calendar" dropdown list.
  5. Click "Import".
  6. A dialog box appears that says something like "2 events imported".
  7. Make sure the calendar from step 4 is visible, and go to 12/1 and 12/2 to see the events.
1
On

If you are sending this ics stream via email, you should have a METHOD:PUBLISH property at the beginning of your VCALENDAR object and follow the rules of iTIP (https://www.rfc-editor.org/rfc/rfc5546) and iMIP (https://www.rfc-editor.org/rfc/rfc6047).

Once you are done, it is also quite possible that some clients (e.g. gmail) do not accept multiple events in the same calendar invitation.

A last (rather aesthetic) comment: the VTIMEZONE component is usually placed at the top of the iCalendar stream.