Writing .ics iCal file using java

*爱你&永不变心* 提交于 2019-12-07 04:19:11

问题


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:uid1@example.com
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
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:uid1@example.com
DTSTAMP:19970714T170000Z
ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
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:uid1@example.com\nDTSTAMP:19970714T170000Z\nORGANIZER;
    CN=John Doe:MAILTO:john.doe@example.com\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();
            }
        }
    }

回答1:


You can use iCal4j API for calendaring.




回答2:


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.




回答3:


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




回答4:


The difference between your file and wikipedia's file is the version number. Try changing version from 1.0 to 2.0. It should work.



来源:https://stackoverflow.com/questions/31238492/writing-ics-ical-file-using-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!