Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading