iCalendar is file format that enables the exchange of calendar events via email etc.A very useful API for creating and managing calendar events and iCalendar files is ical4j and in this tutorial we will see how to create and save a calendar event.

As an example consider the case that we need to create an event that will occur at 5/2/2013 from 9:00 to 17:00.


import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.GregorianCalendar;


import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.Recur;
import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.WeekDay;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.RRule;
import net.fortuna.ical4j.model.property.Uid;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.UidGenerator;


public class CalendarEventTest {

	public static void main(String[] args) throws FileNotFoundException, IOException, ValidationException, ParseException {
		
		//Create an 8 hours event starting at 5/2/2013 9:00 pm ending at 5/2/2013 17:00 am
		
		Calendar start = new GregorianCalendar();
		
		start.set(2013, 2,5,9, 0);
		
		Calendar end=new GregorianCalendar();
		end.set(2013, 2,5,17,0);
		
		DateTime startTime=new DateTime(start.getTime());
		DateTime endTime=new DateTime(end.getTime());
		
		//Create event
		VEvent eightHourEvent=new VEvent(startTime,endTime,"Test Event");
		
		net.fortuna.ical4j.model.Calendar cal = new net.fortuna.ical4j.model.Calendar();
		//add product Id
		cal.getProperties().add(new ProdId("-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN"));
		cal.getProperties().add(Version.VERSION_2_0);
		cal.getProperties().add(CalScale.GREGORIAN);
		
		//generate unique identifier
		UidGenerator ug = new UidGenerator("uidGen");
		Uid uid = ug.generateUid();
		
		eightHourEvent.getProperties().add(uid);

		//add event in ical4j calendar
		cal.getComponents().add(eightHourEvent);
		System.out.println(cal.toString());
		
		
		//save event in test.ics file
		
		 CalendarOutputter out=new CalendarOutputter();
			out.output(cal, new FileOutputStream("C:\\test.ics"));
	}
}

The output of the above snippet will be:

BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20130205T115953Z
DTSTART:20130305T090053
DTEND:20130305T170053
SUMMARY:Test Event
UID:20130205T115954Z-uidGen@javaonly.java-only.com
END:VEVENT
END:VCALENDAR

In case we wanted to make the above event occure every Monday until the end of 2013 we must create a rule that we will add the event.For example

Calendar untilCal = Calendar.getInstance();
		 untilCalendar.set(2013, Calendar.DECEMBER, 31);
		 untilCalendar.set(Calendar.MILLISECOND, 0);
		 DateTime until=new DateTime(untilCal.getTime());
		Recur recur = new Recur(Recur.WEEKLY,until);
		
		 recur.getDayList().add(WeekDay.MO);
		 RRule rrule = new RRule(recur); 
		 eightHourEvent.getProperties().add(rrule);

And the event will now be:

BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20130205T121021Z
DTSTART:20130305T090021
DTEND:20130305T170021
SUMMARY:Test Event
UID:20130205T121021Z-uidGen@javaonly.java-only.com
RRULE:FREQ=WEEKLY;UNTIL=20131231T141021;BYDAY=MO
END:VEVENT
END:VCALENDAR