📜 ⬆️ ⬇️

Java and iCalendar work

Not so long ago, I had to parse and then output the necessary information from the iCalendar . After stubborn searching, I stumbled upon the iCal4J library. Looking closely at its functionality, I realized - this is what I need. Let's try to put it into practice.

Calendar preparation

In Google Calendar, I created a small calendar consisting of several different events ranging from 30 minutes to 6 hours. Then I downloaded the calendar in iCal format to my laptop. If anyone does not know, this is done like this: go to the Google calendar settings and in the section “Private address of the calendar” click on the green button “ICAL”. Everything, calendar is loaded on the device.

Java preparation

The next step was to load the library and connect it to the project. Unpacked and pointed out to the development environment a folder with * .jar library files. Also in the folder with the project was downloaded my downloaded calendar file. Everything is ready to write code!

Download calendar

There are two options for loading the calendar - from a file and parsing the string with the contents of the calendar. We will choose the first method (the second is not significantly different from it).
')
//      FileInputStream fin = new FileInputStream("calendar.ics"); //          Calendar CalendarBuilder builder = new CalendarBuilder(); Calendar calendar = builder.build(fin); 


Show all events

Let's now try to bring somewhere (well, at least to the console) all the events from our calendar.

 //    ComponentList listEvent = calendar.getComponents(Component.VEVENT); for (Object elem : listEvent) { //         ,   listEvent   Object VEvent event = (VEvent) elem; //   // getValue()   ,    .   getName()  toString()       DESCRIPTION String description = event.getDescription().getValue(); //    String title = event.getSummary().getValue(); System.out.println(title + " : " + description); } 

It is worth paying attention to the first line, namely Component.VEVENT. So we specify that we want to receive calendar events. If you do not explicitly indicate what we want to extract from the calendar, we will receive a list that also contains To-Do components (VTODO), notes (VJOURNAL), and other components such as VFREEBUSY, VALARM and VTIMEZONE.

Applying filters

In order to display the elements, in my case of the event, in a certain period of time, you can apply filters.

 java.util.Calendar today = java.util.Calendar.getInstance(); today.set(java.util.Calendar.HOUR_OF_DAY, 0); today.clear(java.util.Calendar.MINUTE); today.clear(java.util.Calendar.SECOND); //     1  Period period = new Period(new DateTime(today.getTime()), new Dur(1, 0, 0, 0)); //      Filter filter = new Filter(new PeriodRule(period)); //     List eventsToday = filter.filter(calendar.getComponents(Component.VEVENT)); // ,  eventsToday   ,     


At the time of writing, the following entries were stored in my calendar (in the format “Title”: “Description” [“Start Time”]):
Event 1: Event 1 - November 1 [7 Nov 2013 13:00:00 GMT]
Event 5: Event 5 - November 12 [12 Nov 2013 12:00:00 GMT]
Event 3: Event 3 - November 10 [10 Nov 2013 19:30:00 GMT]
Event 2: Event 2 - November 9 [9 Nov 2013 08:30:00 GMT]
Event 4: Event 4 - November 11 [11 Nov 2013 08:00:00 GMT]
Event 6: Event 6 - November 13 [13 Nov 2013 09:30:00 GMT]

After applying the filter to display the planned events (November 11), I received only one event - Event 4:
Event 4: Event 4 - November 11 [11 Nov 2013 08:00:00 GMT]

Increasing the period to three days ...
 Period period = new Period(new DateTime(today.getTime()), new Dur(3, 0, 0, 0)); 

... I got this picture:
Event 5: Event 5 - November 12 [12 Nov 2013 12:00:00 GMT]
Event 4: Event 4 - November 11 [11 Nov 2013 08:00:00 GMT]
Event 6: Event 6 - November 13 [13 Nov 2013 09:30:00 GMT]


Creating an empty calendar

iCal4j also supports creating a calendar from scratch. Everything is simple here. Create a calendar object, add the necessary fields to it.

 Calendar calendar = new Calendar(); calendar.getProperties().add(new ProdId("-//habrahabr")); calendar.getProperties().add(Version.VERSION_2_0); calendar.getProperties().add(CalScale.GREGORIAN); 


Adding a new event to the calendar

Here, with the creation of an event, everything is not so simple, but if you look at it, everything will go pretty quickly.

 //    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); TimeZone timezone = registry.getTimeZone("Europe/Minsk"); VTimeZone tz = timezone.getVTimeZone(); //  10   18:00 java.util.Calendar startDate = new GregorianCalendar(); startDate.setTimeZone(timezone); startDate.set(java.util.Calendar.MONTH, java.util.Calendar.NOVEMBER); startDate.set(java.util.Calendar.DAY_OF_MONTH, 10); startDate.set(java.util.Calendar.YEAR, 2013); startDate.set(java.util.Calendar.HOUR_OF_DAY, 18); startDate.set(java.util.Calendar.MINUTE, 0); startDate.set(java.util.Calendar.SECOND, 0); //       20:00 java.util.Calendar endDate = new GregorianCalendar(); endDate.setTimeZone(timezone); endDate.set(java.util.Calendar.MONTH, java.util.Calendar.NOVEMBER); endDate.set(java.util.Calendar.DAY_OF_MONTH, 10); endDate.set(java.util.Calendar.YEAR, 2013); endDate.set(java.util.Calendar.HOUR_OF_DAY, 20); endDate.set(java.util.Calendar.MINUTE, 0); endDate.set(java.util.Calendar.SECOND, 0); //   String eventName = "- "; DateTime start = new DateTime(startDate.getTime()); DateTime end = new DateTime(endDate.getTime()); VEvent meeting = new VEvent(start, end, eventName); //        meeting.getProperties().add(tz.getTimeZoneId()); //    Calendar icsCalendar = new Calendar(); icsCalendar.getProperties().add(new ProdId("-//habrahabr")); icsCalendar.getProperties().add(CalScale.GREGORIAN); //      icsCalendar.getComponents().add(meeting); 


Save calendar to file

After working with the calendar, we certainly need to save it. It is best to save back to the file.

 FileOutputStream fout = new FileOutputStream("calendar.ics"); CalendarOutputter out = new CalendarOutputter(); out.output(calendar, fout); 


Conclusion

We reviewed the basic features of the library iCal4j. Of course, its capabilities are not limited to simply parsing the calendar and creating events. The library also allows you to attach files to events, create events with an indication of the venue in the coordinates, create guest lists and much more. In addition, the library can be used in the development of Android applications.

References:
  1. ICalendar Description
  2. ICal4j Documentation
  3. iCal4j Wiki (here you can download the library)

Source: https://habr.com/ru/post/201660/


All Articles