How do I set an Alarm for a Calendar Event using iCal4j?

Posted on 2011-04-21
Last Modified: 2012-05-11
I am trying to find a simple example of how to add an alarm (or reminder) to an iCal event using iCal4j library.  I would like the alarm to be set 1 hour before the event.  I even know what I want the code to generate (see below), but can't figure out how to make iCal4j do it!


I have simple code below that works great, but there is no alarm component.  The API for iCal4j is not very good and I can't find any examples using google.  Can someone help with this?

private String getIcal2(String from, String to, Date eventDay, String subject,
		String body, String rmdrSummary, String rmdrLocation, String rmdrDescription) {
	// Clean up startDate for "all day" event
	Date startDate;
	Date endDate;
	Calendar cal = Calendar.getInstance();
	cal.set(Calendar.AM_PM, Calendar.AM);
	cal.set(Calendar.HOUR, 0);
	cal.set(Calendar.MINUTE, 0);
	cal.set(Calendar.SECOND, 0);
	cal.set(Calendar.MILLISECOND, 0);
	startDate = new Date(cal.getTimeInMillis());
	cal.add(Calendar.DATE, 1);
	endDate = new Date(cal.getTimeInMillis());
	CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_OUTLOOK_COMPATIBILITY, true);
	// Create Calendar
	net.fortuna.ical4j.model.Calendar cal4j = new net.fortuna.ical4j.model.Calendar();
	cal4j.getProperties().add(new ProdId("-//AFrameSoftware//iCal4j 1.0//EN"));

	// Create event
	VEvent vEvent = new VEvent();
	vEvent.getProperties().add(new Uid("1234"));
	vEvent.getProperties().add(new Summary(rmdrSummary));
	vEvent.getProperties().add(new Description(rmdrDescription));
	vEvent.getProperties().add(new Location(rmdrLocation));
	vEvent.getProperties().add(new DtStart(new DateTime(startDate)));
	vEvent.getProperties().add(new DtEnd(new DateTime(endDate)));
	vEvent.getProperties().add(new XProperty("X-MICROSOFT-CDO-ALLDAYEVENT","TRUE"));
	vEvent.getProperties().add(new XProperty("X-MICROSOFT-CDO-INTENDEDSTATUS","FREE"));
	// !!! ADD ALARM HERE !!!
	// Attach Event to calendar

	return cal4j.toString();


Open in new window

Question by:burtonrhodes
    LVL 47

    Accepted Solution


    Seems to be like that;

    Example 1 - Creating an alarm to trigger at a specific time:

     java.util.Calendar cal = java.util.Calendar.getInstance();
     cal.set(java.util.Calendar.MONTH, java.util.Calendar.DECEMBER);
     cal.set(java.util.Calendar.DAY_OF_MONTH, 25);

     VAlarm christmas = new VAlarm(cal.getTime());

    Example 2 - Creating an alarm to trigger one (1) hour before the scheduled start of the parent event/the parent todo is due:

     VAlarm reminder = new VAlarm(new Dur(0, -1, 0, 0));

     // repeat reminder four (4) more times every fifteen (15) minutes..
     reminder.getProperties().add(new Repeat(4));
     reminder.getProperties().add(new Duration(new Dur(0, 0, 15, 0)));

     // display a message..
     reminder.getProperties().add(new Description("Progress Meeting at 9:30am"));
    LVL 3

    Author Closing Comment

    Not sure how I missed that?!!?  Thanks!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    Set OWA language and time zone in Exchange for individuals, all users or per database.
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now