java 使用java编写.ics iCal文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31238492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:18:44  来源:igfitidea点击:

Writing .ics iCal file using java

javaicalendar

提问by Jarrod Lofy

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.

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

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

我的 .ics 文件

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.

这是用于生成 .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:[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();
            }
        }
    }

回答by Victor Stanciu

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

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

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:

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

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

每行以 CR+LF 结束(十六进制:0D0A)。

Try using \r\ninstead of \n.

尝试使用\r\n而不是\n.

回答by Garry

You can use iCal4j APIfor calendaring.

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

回答by David Bal

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.

我制作了一个有效的 iCalendar API。如果你愿意,你可以重新发明轮子,但我花了 6 个多月的时间才完成。iCalendar 可能比您想象的更复杂。

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

您可以在http://jfxtras.org/ 上查看

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

你可以在https://github.com/JFXtras/jfxtras/tree/8.0/jfxtras-icalendarfx下载

回答by Ashish P Magar

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.

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