如何在Java中手动解析ICS文件?

3

你好,我是一个新手,想要在Java中手动解析ICS(Outlook日历文件)。如何不使用第三方API来解析ICS文件?


欢迎来到SO。很抱歉要开始反驳,但为什么要重新发明轮子呢?特别是如果你不知道如何入手的话?我建议直接使用iCal4j - http://ical4j.sourceforge.net/introduction.html,并将时间用在其他方面。 - mdma
如果你真的想要继续实现这个功能,第一件事就是阅读规范 - http://tools.ietf.org/html/rfc2445 - mdma
2个回答

5

不使用任何第三方库,基本上你需要编写自己的iCalendar (参见RFC 5545)解析器,重现第三方库的工作。这不会很有趣。

诚然,我自己使用iCal4j 的经验并不是非常愉快 - 但我不会从头开始使用java.util.Datejava.util.Calendar来编写自己的解析器。你可能会发现使用Joda Time编写iCalendar解析器来表示各个方面(“日期”,“时间”等)是值得的,因为它比内置的API更好用......但同样地,你可能会发现iCal4j对于你的目的已经足够好了。


0
你可以参考iCal4j的API文档http://ical4j.sourceforge.net/introduction.html 这可能会对你的解析有所帮助,你可以参考这段代码:
{
    Properties prop = new Properties();

            prop.put("mail.smtp.auth", "true");
            prop.put("mail.smtp.starttls.enable", "true");
            prop.put("mail.smtp.host", "smtp.gmail.com");
            prop.put("mail.smtp.port", "587");
            final String username = "xx@xx"; //your email id
            final String password = "password"; //your password
            System.out.println(meeting);
             try {
                 Session session = Session.getInstance(prop, new javax.mail.Authenticator() {
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(username, password);
                        }
                    });

                    MimeMessage mimeMessage = new MimeMessage(session);
        Multipart multipart = new MimeMultipart("alternative");
        if (calendar != null) {
                        // Another part for the calendar invite
                        MimeBodyPart invite = new MimeBodyPart();
                        invite.setHeader("Content-Class", "urn:content-  classes:calendarmessage");
                        invite.setHeader("Content-ID", "calendar_message");
                        invite.setHeader("Content-Disposition", "inline");
                        invite.setContent(calendar.toString(), "text/calendar");
                        multipart.addBodyPart(invite);
                    }

                    mimeMessage.setContent(multipart);

                    Transport.send(mimeMessage);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接