Digital Marketing

Java Calendar iCal4j example

iCalendar is a computer file format which allows Internet users to send meeting requests and tasks to other Internet users, via email, or sharing files with an extension of .ics. Recipients of the iCalendar data file (with supporting software, such as an email client or calendar application) can respond to the sender easily or counter-propose another meeting date/time.

iCalendar is used and supported by a large number of products, including Google Calendar, Apple Calendar (formerly iCal), IBM Lotus Notes, Yahoo! Calendar, Evolution (software), eM Client, Lightning extension for Mozilla Thunderbird and SeaMonkey, and partially by Microsoft Outlook and Novell GroupWise.

iCalendar is designed to be independent of the transport protocol. For example, certain events can be sent by traditional email or whole calendar files can be shared and edited by using a WebDav server, or SyncML. Simple web servers (using just the HTTP protocol) are often used to distribute iCalendar data about an event and to publish busy times of an individual. Publishers can embed iCalendar data in web pages using hCalendar, a 1:1 microformat representation of iCalendar in semantic (X)HTML.

 iCal4j is an API which is used to modifying existing iCalendar data or creating new iCalendar data. creates a .ics file lets say mycalendar.ics. It also creates a calender event.

package ca.i88.example.ical4j;

import java.util.Iterator;
import net.fortuna.ical4j.model.Component;
import net.fortuna.ical4j.model.Date;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.parameter.Value;
import net.fortuna.ical4j.util.UidGenerator;

* @author
public class Ical4jExample {

public static void main(String[] args) throws IOException, ParserException, ValidationException {

String calFile = "mycalendar.ics";

//Creating a new calendar
net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
calendar.getProperties().add(new ProdId("-//I88CA//i88ca 1.88//EN"));

//Creating an event
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(java.util.Calendar.MONTH, java.util.Calendar.JULY);
cal.set(java.util.Calendar.DAY_OF_MONTH, 1);

VEvent christmas = new VEvent(new Date(cal.getTime()), "Canada Day");
// initialise as an all-day event..

UidGenerator uidGenerator = new UidGenerator("1");


//Saving an iCalendar file
FileOutputStream fout = new FileOutputStream(calFile);

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

//Now Parsing an iCalendar file
FileInputStream fin = new FileInputStream(calFile);

CalendarBuilder builder = new CalendarBuilder();

calendar =;

//Iterating over a Calendar
for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
Component component = (Component);
System.out.println("Component [" + component.getName() + "]");

for (Iterator j = component.getProperties().iterator(); j.hasNext();) {
Property property = (Property);
System.out.println("Property [" + property.getName() + ", " + property.getValue() + "]");


Popular posts from this blog

Make online money from the Internet