This quick start guide will show you the basics of how to read and write iCalendar objects using biweekly. We will be using the Biweekly
class to do this. This class contains a collection of static factory methods that use method chaining to simplify the reading/writing process.
Call the Biweekly.parse()
method to parse an iCalendar object. The iCalendar object can be parsed from a String
, File
, InputStream
, or Reader
.
String str = "BEGIN:VCALENDAR\r\n" + "VERSION:2.0\r\n" + "PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN\r\n" + "BEGIN:VEVENT\r\n" + "UID:0123\r\n" + "DTSTAMP:20130601T080000Z\r\n" + "SUMMARY;LANGUAGE=en-us:Team Meeting\r\n" + "DTSTART:20130610T120000Z\r\n" + "RRULE:FREQ=WEEKLY;INTERVAL=2\r\n" + "END:VEVENT\r\n" + "END:VCALENDAR\r\n"; //parse the first iCalendar object from the data stream ICalendar ical = Biweekly.parse(str).first(); //or parse all objects from the data stream //List<ICalendar> icals = Biweekly.parse(str).all(); VEvent event = ical.getEvents().get(0); String summary = event.getSummary().getValue();
To create a new iCalendar object, simply create a new instance of the ICalendar
class, and populate it with data. Then, pass it into the Biweekly.write()
method, and call go()
. The iCalendar object can be written to a String
, File
, OutputStream
, or Writer
.
ICalendar ical = new ICalendar(); VEvent event = new VEvent(); Summary summary = event.setSummary("Yearly Conference"); summary.setLanguage("en-us"); Date start = ... event.setDateStart(new DateStart(start, false)); event.setDuration(new Duration.Builder().days(1).builder()); //all-day event Recurrence recur = new Recurrence.Builder(Frequency.YEARLY).build(); event.setRecurrenceRule(recur); ical.addEvent(event); File file = new File("conference.ics"); Biweekly.write(ical).go(file);
ICalendar
objectTo help ensure that data within an ICalendar
Java object properly adheres to the iCalendar specifications, the validate()
method can be called. This method returns a list of warnings, alerting the developer to such things as required fields being missing (such as the ACTION
property in a VALARM
component) or other data inconsistencies (such as the date in a DTSTART
property coming after the date in a DTEND
property).
The presence of validation warnings will not prevent the ICalendar
object from being written out to a data stream (a syntactically-correct iCalendar object will still be produced). However, the application that consumes the iCalendar object may have trouble interpreting some of the data, due to the presence of these warnings.
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); ICalendar ical = new ICalendar(); VEvent event = new VEvent(); event.setDateStart(df.parse("2013-07-15 13:00")); event.setDateEnd(df.parse("2013-07-15 11:00")); ical.addEvent(event); ValidationWarnings warnings = ical.validate(); System.out.println(warnings.toString()); //produces this warning: //[ICalendar > VEvent]: DateStart must come before DateEnd.
Hi i am a newbie here ,I need to save and retrieve the recurrences into a database like mysql, can someone help .