Skip to content

Examples

Mike Angstadt edited this page Oct 26, 2017 · 10 revisions

Contents
Parsing an iCalendar file
Creating an iCalendar file
Creating an all-day event
Cancelling an event
Formatting dates under a Timezone
Calculating the dates in a recurring event
Parsing an RRULE string

Parsing an iCalendar file

//using Biweekly
File file = ...
List<ICalendar> icals = Biweekly.parse(file).all();

//using ICalReader
File file = ...
ICalReader reader = null;
try {
  reader = new ICalReader(file);
  ICalendar ical;
  while ((ical = reader.readNext()) != null){
    ...
  }
} finally {
  if (reader != null) reader.close();
}

Creating an iCalendar file

ICalendar ical = new ICalendar();
VEvent event = new VEvent();
event.setSummary("the summary");
...
ical.addEvent(event);

//using Biweekly
File file = ...
Biweekly.write(ical).go(file);

//using ICalWriter
File file = ...
ICalWriter writer = null;
try {
  writer = new ICalWriter(file);
  writer.write(ical);
} finally {
  if (writer != null) writer.close();
}

Creating an all-day event

See: RFC 5546 Section 4.1.5

VEvent event = new VEvent();
Date start = ...
event.setDateStart(start, false);

Cancelling an event

See: RFC 5546 Section 4.1.3

ICalendar ical = new ICalendar();
ical.setMethod("CANCEL");
VEvent event = new VEvent();
event.setOrganizer("jdoe@example.com"); //the organizer of the existing event
event.setUid(...); //the UID of the existing event
event.setSequence(2); //increment the sequence number of the existing event
event.addComment(...); //optional: describe why it was cancelled
ical.addEvent(event);

Formatting all dates under a Timezone

By default, biweekly writes all date/time values in UTC time, but this can be changed. See Timezones for more information on how to work with timezones.

TimeZone javaTz = TimeZone.getTimeZone("America/New_York");
TimezoneAssignment newYork = TimezoneAssignment.download(javaTz, false);

ICalendar ical = ...
ical.getTimezoneInfo().setDefaultTimezone(newYork);

File file = ...
ICalWriter writer = null;
try {
  writer = new ICalWriter(file);
  writer.write(ical);
} finally {
  if (writer != null) writer.close();
}

Calculating the dates in a recurring event

ICalendar ical = ...
TimezoneInfo tzinfo = ical.getTimezoneInfo();

VEvent event = ical.getEvents().get(0);
DateStart dtstart = event.getDateStart();

TimeZone timezone;
if (tzinfo.isFloating(dtstart)){
  timezone = TimeZone.getDefault();
} else {
  TimezoneAssignment dtstartTimezone = tzinfo.getTimezone(dtstart);
  timezone = (dtstartTimezone == null) ? TimeZone.getTimeZone("UTC") : dtstartTimezone.getTimeZone();
}
DateIterator it = event.getDateIterator(timezone);

Parsing an RRULE string

RecurrenceRuleScribe scribe = new RecurrenceRuleScribe();
ParseContext context = new ParseContext();
context.setVersion(ICalVersion.V2_0);
RecurrenceRule rrule = scribe.parseText("FREQ=WEEKLY;INTERVAL=2", null, new ICalParameters(), context);
Recurrence recur = rrule.getValue();

Date start = ...
TimeZone timezone = ...
DateIterator it = rrule.getDateIterator(start, timezone);
while (it.hasNext()) {
    System.out.println(it.next());
}