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.

ICalendarExample.java creates a .ics file lets say mycalendar.ics. It also creates a calender event.

package ca.i88.example.ical4j;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import net.fortuna.ical4j.data.CalendarBuilder;
import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.data.ParserException;
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.model.property.CalScale;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.UidGenerator;

/**
*
* @author goyun.info
*/
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"));
calendar.getProperties().add(Version.VERSION_2_0);
calendar.getProperties().add(CalScale.GREGORIAN);

//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..
christmas.getProperties().getProperty(Property.DTSTART).getParameters().add(Value.DATE);

UidGenerator uidGenerator = new UidGenerator("1");
christmas.getProperties().add(uidGenerator.generateUid());

calendar.getComponents().add(christmas);

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

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

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

CalendarBuilder builder = new CalendarBuilder();

calendar = builder.build(fin);

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

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



Comments

Popular posts from this blog

Make online money from the Internet