使用java编写.ics iCal文件

时间:2015-07-06 05:47:36

标签: java icalendar

我正在尝试使用java实现我自己的iCal创建者,由于某种原因,我无法识别我的.ics文件。我想知道我做错了什么我可以获得看起来与维基百科的例子完全一样的输出。 .ics文件和我的程序生成的文件有什么区别。

他们的例子:

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

我的.ics文件

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 

这是用于生成.ics文件的代码。

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

4 个答案:

答案 0 :(得分:2)

显然,vCalendar中的所有行都不允许以空格字符结尾。

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

如果删除这些空格,则格式为validates

编辑:另外,来自iCalendar上的维基百科条目:

  

每行由CR + LF终止(十六进制:0D0A)。

尝试使用\r\n代替\n

答案 1 :(得分:2)

您可以使用iCal4j API进行日历。

答案 2 :(得分:0)

我制作了一个可行的iCalendar API。如果你愿意,你可以重新发明轮子,但是我花了6个多月才完成它。您认为iCalendar可能更复杂。

您可以在http://jfxtras.org/

查看

您可以在https://github.com/JFXtras/jfxtras/tree/8.0/jfxtras-icalendarfx

下载

答案 3 :(得分:0)

您的文件和维基百科文件之间的区别在于版本号。尝试将版本从1.0更改为2.0。它应该工作。