javaical4j

ical4j - create a new calendar


I am trying to create a calendar using ical4j library.

I am getting error. Code and error below. Not sure what I am doing wrong but I am following the same code snippet that was provided in examples and documentation.

Code:

package generate_icalendar;

import net.fortuna.ical4j.model.Calendar;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Version;

public class test {
    public static void main(String[] args) {
        Calendar calendar = new Calendar();
        calendar.getProperties().add(new ProdId("-//Test//iCal4j 1.0//EN"));
        calendar.getProperties().add(Version.VERSION_2_0);
        calendar.getProperties().add(CalScale.GREGORIAN);
    }
}

Error:

run:
Exception in thread "main" java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.add(Collections.java:1057)
    at generate_icalendar.test.main(test.java:19)
C:\Users\anil\AppData\Local\NetBeans\Cache\12.5\executor-snippets\run.xml:111: The following error occurred while executing this line:
C:\Users\anil\AppData\Local\NetBeans\Cache\12.5\executor-snippets\run.xml:94: Java returned: 1
BUILD FAILED (total time: 1 second)

I am using the below listed JARs in my project:

commons-lang3-3.12.0.jar
slf4j-api-2.0.0.jar
commons-collections4-4.4.jar
commons-codec-1.15.jar
ical4j-4.0.0-alpha12.jar

I am using JDK 1.8

Please help me understand what I am doing wrong.


Solution

  • Your code is for an older version of the library. If you look at the javadoc for 4.0.0-alpha12 you can see what you're doing wrong. http://ical4j.github.io/docs/ical4j/api/4.0.0-alpha12/net/fortuna/ical4j/model/Calendar.html

     Calendar calendar = new Calendar();
     calendar.add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
     calendar.add(Version.VERSION_2_0);
     calendar.add(CalScale.GREGORIAN);
    

    Your code was valid for ical4j 3, but you're using an alpha version of ical4j 4.