Question

I am creating a PC program which is based on iCalendar format. I need to be able to get the data from a current ics file and display it as a calendar or at least something similar to calendar. I know how to get the data from ics file just don't have any idea how to easily use that data for displaying purposes.

Here is my starting code:

public void getCalendarData(File f) throws FileNotFoundException, IOException, ParserException
{
    FileInputStream fin = new FileInputStream(f);
    builder = new CalendarBuilder();
    calendar = builder.build(fin);
}
Was it helpful?

Solution

One thing is ical4j, which is basically a utility that wraps the ICS format.

Another thing is a front end for a calendar/schedule :-)

But, lucky us, there's a nice JSF component with Primefaces, that you can use if a web interface is OK for you.

http://www.primefaces.org/showcase/ui/data/schedule.xhtml

Basically, what you need, is to just parse the data from ICS and feed the primefaces component data model (the link above has both the JSF and the managed bean example of how to use the component)

So you'd have to so something like this

private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd");

@PostConstruct
private void loadIcs() {
    eventModel = new DefaultScheduleModel();
    CalendarBuilder builder = new CalendarBuilder();

    try {
        net.fortuna.ical4j.model.Calendar calendar = builder.build(this.getClass().getResourceAsStream("canada.ics"));

        for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
            Component component = (Component) i.next();
            //new event
            Date start = SDF.parse(component.getProperty("DTSTART").getValue());
            Date end = SDF.parse(component.getProperty("DTEND").getValue());
            String summary = component.getProperty("SUMMARY").getValue();

            eventModel.addEvent(new DefaultScheduleEvent(summary,
            start, end));

            System.out.println("added "+start+end+summary);

        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

OTHER TIPS

Not sure if you mean the actual GUI or getting the list of dates from an icalendar which is fairly complex given the RRULE properties. In ther first case, the choice is wide open (HTML, ...) in the later case there is an example here copied below :

// Reading the file and creating the calendar
CalendarBuilder builder = new CalendarBuilder();
Calendar cal = null;
try {
    cal = builder.build(new FileInputStream("my.ics"));
} catch (IOException e) {
    e.printStackTrace();
} catch (ParserException e) {
    e.printStackTrace();
}


// Create the date range which is desired.
DateTime from = new DateTime("20100101T070000Z");
DateTime to = new DateTime("20100201T070000Z");;
Period period = new Period(from, to);


// For each VEVENT in the ICS
for (Object o : cal.getComponents("VEVENT")) {
    Component c = (Component)o;
    PeriodList list = c.calculateRecurrenceSet(period);

    for (Object po : list) {
        System.out.println((Period)po);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top