Monday, May 21, 2012

iCal4j example - Set a Meeting using iCalendar

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 iCal,IBM Lotus Notes,Yahoo Calendar, Microsoft Outlook etc

iCal4j is an API which is used to modifying existing iCalendar data or creating new iCalendar data. To know more about iCal4j, click here.

About the example

This example is straight forward, we will create a calendar (net.fortuna.ical4j.model.Calendar) object and add a Meeting event to it. You can set various properties to the event like subject, location, description etc. Obviously you can customize it according to your requirement. This example will generate a .ics file (TestCalendar.ics). You can send this generated file via email to interested parties. Please see the below self explanatory java program.

For running below example you will need to add below mentioned JAR files in your classpath.
  • ical4j-1.0.3.jar
  • backport-util-concurrent-3.1.jar
  • commons-logging-1.1.1.jar
  • commons-lang-2.6.jar

To download the above JAR file, click here.
/* ICalendarExample.java */

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.Dur;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Description;
import net.fortuna.ical4j.model.property.Location;
import net.fortuna.ical4j.model.property.Organizer;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;

public class ICalendarExample {

 public static void main(String[] args) {
  
  //Initilize values
  String calFile = "TestCalendar.ics";
  
  //start time
  java.util.Calendar startCal = java.util.Calendar.getInstance();
  startCal.set(2012, 04, 23, 20, 00);
  
  //end time
  java.util.Calendar endCal = java.util.Calendar.getInstance();
  endCal.set(2012, 04, 23, 20, 30);
  
  String subject = "Meeting Subject";
  String location = "Location - Mumbai";
  String description = "This goes in decription section of the metting like agenda etc.";
  
  String hostEmail = "admin@javaxp.com";
  
  //Creating a new calendar
  net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
  calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
  calendar.getProperties().add(Version.VERSION_2_0);
  calendar.getProperties().add(CalScale.GREGORIAN);
  
  SimpleDateFormat sdFormat =  new SimpleDateFormat("yyyyMMdd'T'hhmmss'Z'");
  String strDate = sdFormat.format(startCal.getTime());
  
  net.fortuna.ical4j.model.Date startDt = null;
  try {
   startDt = new net.fortuna.ical4j.model.Date(strDate,"yyyyMMdd'T'hhmmss'Z'");
  } catch (ParseException e) {
   e.printStackTrace();
  }
  
  long diff = endCal.getTimeInMillis() - startCal.getTimeInMillis();
  int min = (int)(diff / (1000 * 60));
  
  Dur dur = new Dur(0,0,min,0);
  
  //Creating a meeting event
  VEvent meeting = new VEvent(startDt,dur,subject);
  
  meeting.getProperties().add(new Location(location));
  meeting.getProperties().add(new Description());
  
  try {
   meeting.getProperties().getProperty(Property.DESCRIPTION).setValue(description);
  } catch (IOException e) {
   e.printStackTrace();
  } catch (URISyntaxException e) {
   e.printStackTrace();
  } catch (ParseException e) {
   e.printStackTrace();
  }
  
  try {
   meeting.getProperties().add(new Organizer("MAILTO:"+hostEmail));
  } catch (URISyntaxException e) {
   e.printStackTrace();
  }
  
  calendar.getComponents().add(meeting);
  
  FileOutputStream fout = null;
  
  try {
   fout = new FileOutputStream(calFile);
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  
  CalendarOutputter outputter = new CalendarOutputter();
  outputter.setValidating(false);
  
  try {
   outputter.output(calendar, fout);
  } catch (IOException e) {
   e.printStackTrace();
  } catch (ValidationException e) {
   e.printStackTrace();
  }
  
  System.out.println(meeting);
 }
}

Generated file TestCalendar.ics

TestCalendar.ics

BEGIN:VCALENDAR
PRODID:-//Ben Fortuna//iCal4j 1.0//EN
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20120521T121647Z
DTSTART;VALUE=DATE:20120523T120000Z
DURATION:PT30M
SUMMARY:Meeting Subject
LOCATION:Location - Mumbai
DESCRIPTION:This goes in decription section of the metting like agenda et c.
ORGANIZER:MAILTO:admin@javaxp.com
END:VEVENT
END:VCALENDAR