Writing .ics iCal file using java

Jarrod Lofy picture Jarrod Lofy · Jul 6, 2015 · Viewed 19.9k times · Source

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();
            }
        }
    }

Answer

Garry picture Garry · Jul 6, 2015

You can use iCal4j API for calendaring.