我正在创建一个基于 iCalendar 格式的 PC 程序。我需要能够从当前的 ics 文件中获取数据并将其显示为日历或至少类似于日历的内容。我知道如何从 ics 文件中获取数据只是不知道如何轻松地将这些数据用于显示目的。

这是我的起始代码:

public void getCalendarData(File f) throws FileNotFoundException, IOException, ParserException
{
    FileInputStream fin = new FileInputStream(f);
    builder = new CalendarBuilder();
    calendar = builder.build(fin);
}

最佳答案

有一件事是 ical4j,它基本上是一个包装 ICS 格式的实用程序。

另一件事是日历/日程表的前端:-)

但是,幸运的是,有一个带有 Primefaces 的不错的 JSF 组件,如果 Web 界面适合您,您可以使用它。

http://www.primefaces.org/showcase/ui/data/schedule.xhtml

基本上,您所需要的只是解析来自 ICS 的数据并提供primefaces组件数据模型(上面的链接既有JSF也有如何使用组件的托管bean示例)

所以你必须像这样

private static final SimpleDateFormat SDF = new SimpleDateFormat("yyyyMMdd");

@PostConstruct
private void loadIcs() {
    eventModel = new DefaultScheduleModel();
    CalendarBuilder builder = new CalendarBuilder();

    try {
        net.fortuna.ical4j.model.Calendar calendar = builder.build(this.getClass().getResourceAsStream("canada.ics"));

        for (Iterator i = calendar.getComponents().iterator(); i.hasNext();) {
            Component component = (Component) i.next();
            //new event
            Date start = SDF.parse(component.getProperty("DTSTART").getValue());
            Date end = SDF.parse(component.getProperty("DTEND").getValue());
            String summary = component.getProperty("SUMMARY").getValue();

            eventModel.addEvent(new DefaultScheduleEvent(summary,
            start, end));

            System.out.println("added "+start+end+summary);

        }
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParserException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

关于java - 如何使用 ics 文件中的数据构建日历 View ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21742694/

10-11 04:04
查看更多