Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a calendar view using data from ics file?

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);
}
like image 978
Limeran Avatar asked Oct 22 '25 07:10

Limeran


1 Answers

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();
    }

}
like image 74
Leo Avatar answered Oct 23 '25 19:10

Leo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!