【问题标题】:ical4j - Find Event based on the UIDical4j - 根据 UID 查找事件
【发布时间】:2018-09-11 22:16:11
【问题描述】:

我正在使用 ical4j 创建我的 .ical - 文件并保存事件。 但是如何在日历中找到存储的 VEvent?

我有以下代码,但它不起作用? 我得到了我的日历 -> 这正在工作,我已经在调试它了

public VEvent findEvent(CalendarExtern calendarExtern, String hashId) throws IOException, ParserException {

    Calendar calendar = readCalenderFromFile(calendarExtern);

    for (Component component : calendar.getComponents(Component.VEVENT)) {
          if (hashId.equals(component.getProperty(Property.UID))) {
              VEvent event = (VEvent) component;
                return event;
          }
        } 

    return null;
}

有什么想法吗?非常感谢

【问题讨论】:

    标签: java calendar ical4j


    【解决方案1】:

    component.getProperty(Property.UID) 返回一个 Property 所以你真正想做的是:

    if (hashId.equals(component.getProperty(Property.UID).getValue()))...
    

    当然,根据您的输入,您可能希望在执行 getValue() 之前检查没有 UID 属性的组件。

    【讨论】:

    • 非常感谢
    • 另请注意,如果您正在阅读支持修订的日历,则可能存在多个具有指定 UID 的事件。在这种情况下,您可能需要使用ComponentGroup 来查找特定事件的最新版本。
    【解决方案2】:

    使用ComponentGroup 查找事件的最新版本的替代方法:

    ComponentGroup<VEvent> group = new ComponentGroup(
             calendar.getComponents(Component.VEVENT),
             new Uid(hashId));
    
    return group.getLatestRevision();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-04
      相关资源
      最近更新 更多