Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing .ics iCal file using java

Tags:

java

icalendar

I am attempting to implement my own iCal creator using java and for some reason I can't get my .ics file to be recognized. I was wondering what I am doing wrong I can get output that looks exactly like the example from wikipedia. What is the difference between the .ics file and the once that my program has generated.

Their Example:

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:[email protected]
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT
END:VCALENDAR

My .ics file

BEGIN:VCALENDAR 
VERSION:1.0 
PRODID://Elara/lofy/tanare/delp/314sum2015// 
BEGIN:VEVENT 
UID:[email protected]
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:[email protected]
DTSTART:19970714T170000Z
DTEND:19970715T035959Z
SUMMARY:Bastille Day Party
END:VEVENT 
END:VCALENDAR 

This is the code used to generate the .ics file.

    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;

    public class iCal {

    private String version =    "VERSION:1.0 \n";
    private String prodid =     "PRODID://Elara/lofy/tanare/delp/314sum2015// \n";
    private String calBegin =   "BEGIN:VCALENDAR \n";
    private String calEnd =     "END:VCALENDAR \n";
    private String eventBegin = "BEGIN:VEVENT \n";
    private String eventEnd =   "END:VEVENT \n";

        public void iCal(){
        }

        public void write( String name ){
            StringBuilder builder = new StringBuilder();
            builder.append(name);
            builder.append(".ics");

    String testExample = "UID:[email protected]\nDTSTAMP:19970714T170000Z\nORGANIZER;
    CN=John Doe:MAILTO:[email protected]\nDTSTART:19970714T170000Z
    \nDTEND:19970715T035959Z\nSUMMARY:Bastille Day Party\n";

            try {

                File file = new File(builder.toString());

                // if file doesnt exists, then create it
                if (!file.exists()) {
                    file.createNewFile();
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(calBegin);
                bw.write(version);
                bw.write(prodid);
                bw.write(eventBegin);
                bw.write(testExample);
                bw.write(eventEnd);
                bw.write(calEnd);

                bw.close();

                System.out.println("Done");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
like image 971
Jarrod Lofy Avatar asked Jul 06 '15 05:07

Jarrod Lofy


People also ask

What is ICS file in Java?

An ICS file is an iCalendar file having . iCal4j is a Java library used to read and write iCalendar data streams. The iCalendar standard provides a common data format for storing information about calendar specific events data.

What is ICS file in Java?

An ICS file is an iCalendar file having .ics extension. An ICS file contains plain text with calendar event details such as starting and ending time of events, event description, etc. In this example, we will use iCal4j to create an ICS file in Java. iCal4j is a Java library used to read and write iCalendar data streams.

What is iCal4j in Java?

iCal4j is a Java library used to read and write iCalendar data streams as defined in RFC2445. The iCalendar standard provides a common data format used to store information about calendar-specific data such as events, appointments, to-do lists, etc.

How can I create ics files algorithmically?

If you need to create ICS files algorithmically you can use one of the many available development libraries which can output information in iCalendar format. These include: LIBICAL – Free and open-source C++ library which can read, change and write back data in iCalendar format.

How to export calendar information to ICS file?

You can either use one of the applications which allows export of calendar information into ICS file, use development library to write out iCalendar information, convert file in some other format (CSV for example) to ICS, or type ICS file manually. There are many calendar applications which allow export of events into ICS file.


2 Answers

Apparently not all the lines in a vCalendar are allowed to end with a space character.

BEGIN:VCALENDAR  <- There is a space here
...
BEGIN:VEVENT  <- Here too
...
END:VEVENT  <- Ditto
END:VCALENDAR  <- Last one

If you remove those spaces, your format validates.

Edit: Also, from the Wikipedia entry on iCalendar:

Each line is terminated by CR+LF (in hexadecimal: 0D0A).

Try using \r\n instead of \n.

like image 178
Victor Stanciu Avatar answered Sep 19 '22 17:09

Victor Stanciu


I made an iCalendar API that works. You can reinvent the wheel if you want, but it took me over 6 months to get it done. iCalendar may be more complicated that you think.

You can check it out at http://jfxtras.org/

You can download it at https://github.com/JFXtras/jfxtras/tree/8.0/jfxtras-icalendarfx

like image 42
David Bal Avatar answered Sep 20 '22 17:09

David Bal