I
have planned to write an article for iCal – Calendar event utility using Java
technology long back. Due to many reasons, it took some time to prepare this
article. I have done with the working POC. You can explore and come back if you
have any doubts in iCalendar generation using iCal4j framework.
iCal4j
can be used to creating new iCalendar data from scratch OR modifying existing
iCalendar data. Here you will find a few examples indicating the recommended
usage of this library.
iCal
comes with many handy functionality to deal with iCalendar related
functionalities. Major functionalities are:
Creating an iCal
event
Updating an existing
iCal event
Parsing anexisting
iCalendar file
Iterating over a
Calendar and get the available events
It
also provides a functionality to deal with different TimeZones. Here I have
added my POC code only for iCalendar file creation alone using iCal framework.
You can use this as a reference. Rest of the functionalities will be easy if
you understand the iCalendar creation. Those functionalise can be explored at http://ical4j.sourceforge.net/introduction.html
Creating an iCal
event – Sample Code:
public static
void main(String[] args) throws
FileNotFoundException,
IOException,
ValidationException, ParserException {
String calFile = "c:/mycalendar.ics";
//
Creating a new calendar
net.fortuna.ical4j.model.Calendar
calendar = new
net.fortuna.ical4j.model.Calendar();
calendar.getProperties().add(
new
ProdId("-//Temp//iCal4j 1.0//EN"));
//
calendar.getProperties().add(Version.VERSION_2_0);
calendar.getProperties().add(CalScale.GREGORIAN);
Calendar calStart = new
GregorianCalendar().getInstance();
calStart.set(2015,
java.util.Calendar.MARCH
, 03, 12, 0);
Calendar calEnd = new
GregorianCalendar();
calEnd.set(2015,
java.util.Calendar.MARCH,
03, 13, 0);
DateTime startTime = new
DateTime(calStart.getTime());
DateTime endTime = new
DateTime(calEnd.getTime());
String appmtSubject = "Test
appointment_EventId12345";
//
Create event
VEvent appointmentEvent
= new VEvent(startTime, endTime,
appmtSubject);
appointmentEvent.getProperties().add(new
Location("NC Home"));
StringBuilder
appmtSumamry = new
StringBuilder();
appmtSumamry.append("Dear
Friend, \n\n");
appmtSumamry.append("This
is to catchup with you for 30 mins at my home. \n");
appmtSumamry.append("Topic:
Chumma\n");
appmtSumamry.append("Address:
address1, address2, postcode\n");
appmtSumamry.append("Telephone:
\n");
appmtSumamry.append("Purpose:
\n\n");
appmtSumamry.append("if
you need any assistance mean time, please contact us at 1800XXXYYYY");
appointmentEvent.getProperties().add(new
Description(appmtSumamry.toString()));
/*//
initialise as an all-day event..
christmas.getProperties().getProperty(Property.DTSTART).getParameters()
.add(Value.DATE);*/
UidGenerator
uidGenerator = new
UidGenerator("1");
appointmentEvent.getProperties().add(uidGenerator.generateUid());
calendar.getComponents().add(appointmentEvent);
//
Saving an iCalendar file
FileOutputStream fout = new
FileOutputStream(calFile);
CalendarOutputter
outputter = new
CalendarOutputter();
outputter.setValidating(false);
outputter.output(calendar,
fout);
Required
JARs:
commons-lang-2.6.jar
commons-logging-1.1.3.jar
ical4j-1.0.5.jar
backport-util-concurrent-3.1.jar
If you are seeking for any specific details to build a Calendar event using iCal, feel free to comment this blog. I am happy to respond to you.
Thanks,
Nanjundan Chinnasamy