Searching...
Saturday, 23 November 2013

How to create Calendar event in java ?

21:40

iCal4j is a Java API that provides support for the iCalendar specification. Here is one simple example to create calendar file using Ical4j.

1. Create a Maven project and add below dependency.

	         <dependency>
			<groupId>org.mnode.ical4j</groupId>
			<artifactId>ical4j</artifactId>
			<version>1.0.2</version>
		</dependency>

2. Create a Main class

package com.pretech;
import java.io.FileOutputStream;
import java.util.Date;
import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.DateTime;
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.Uid;
import net.fortuna.ical4j.model.property.Version;
import net.fortuna.ical4j.util.UidGenerator;
public class JavaCalendarExample {
	public static void main(String args[]) {
		try {
		
			Date sdata=new Date("01-JAN-2014");
			Date edata=new Date("01-FEB-2014");
			DateTime startTime = new DateTime(sdata.getTime());
			DateTime endTime = new DateTime(edata.getTime());
			VEvent newyearProgram = new VEvent(startTime, endTime, "New Year");
			net.fortuna.ical4j.model.Calendar cal = new net.fortuna.ical4j.model.Calendar();
			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);
			UidGenerator ug = new UidGenerator("uidGen");
			Uid uid = ug.generateUid();
			newyearProgram.getProperties().add(uid);
			cal.getComponents().add(newyearProgram);
			System.out.println("Calendar created "+cal.toString());
			CalendarOutputter out = new CalendarOutputter();
			out.output(cal, new FileOutputStream("C:\\newyear.ics"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

3. Output


1. Console :



Calendar created BEGIN:VCALENDAR
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN
VERSION:2.0
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTAMP:20131124T094411Z
DTSTART:20140101T000000
DTEND:20140201T000000
SUMMARY:New Year
UID:20131124T094411Z-uidGen@fe80:0:0:0:c925:b5ba:6c70:2626%10
END:VEVENT
END:VCALENDAR


2. Open c:\newyear.ics  



image

0 comments: