我使用 Zimbra 日历的导出文件,并希望创建一个每天发生的所有事件的列表。简化的日历看起来像这样。
BEGIN:VCALENDAR
X-WR-CALNAME:Test-Parking
VERSION:2.0
BEGIN:VTIMEZONE
TZID:Europe/Berlin
BEGIN:STANDARD
DTSTART:19710101T030000
TZOFFSETTO:+0100
TZOFFSETFROM:+0200
RRULE:FREQ=YEARLY;WKST=MO;INTERVAL=1;BYMONTH=10;BYDAY=-1SU
TZNAME:CET
END:STANDARD
END:VTIMEZONE
BEGIN:VEVENT
RRULE:FREQ=DAILY;COUNT=3;INTERVAL=1
SUMMARY:Day Event 3 Times
DTSTART;VALUE=DATE:20120911
DTEND;VALUE=DATE:20120912
END:VEVENT
BEGIN:VEVENT
SUMMARY:4 hours event
DTSTART;TZID="Europe/Berlin":20120911T090000
DTEND;TZID="Europe/Berlin":20120911T130000
END:VEVENT
END:VCALENDAR
现在我写了几行代码来过滤事件:
public class CalendarTester {
private Calendar calendar;
private TimeZoneRegistry registry;
public static void main(String[] args) throws Exception {
CalendarTester tester = new CalendarTester();
tester.run();
}
private void run() throws IOException, ParserException, URISyntaxException,
ParseException {
readCalendar();
TimeZone tz = registry.getTimeZone("Europe/Berlin");
SimpleDateFormat formatter = new SimpleDateFormat();
GregorianCalendar cal = new GregorianCalendar(2012,
GregorianCalendar.SEPTEMBER, 10);
cal.set(GregorianCalendar.HOUR_OF_DAY, 0);
cal.set(GregorianCalendar.MINUTE, 0);
cal.set(GregorianCalendar.SECOND, 0);
cal.setTimeZone(tz);
for (int i = 0; i < 5; i++) {
Period p = new Period(new DateTime(cal.getTime()), new Dur(1, 0, 0,
0));
p.setTimeZone(tz);
Rule[] rules = new Rule[1];
rules[0] = new PeriodRule(p);
Filter f = new Filter(rules, Filter.MATCH_ALL);
System.out.println("_____________");
System.out.println(formatter.format(cal.getTime()));
for (Object event : f.filter(calendar
.getComponents(Component.VEVENT))) {
printEvent((VEvent) event, formatter);
}
cal.add(GregorianCalendar.DAY_OF_YEAR, 1);
}
}
private void printEvent(VEvent event, SimpleDateFormat formatter) {
System.out.println("Start: "
+ formatter.format(event.getStartDate().getDate()) + " End: "
+ formatter.format(event.getEndDate().getDate()) + " Summary: "
+ event.getSummary().getValue());
}
private void readCalendar() throws FileNotFoundException, IOException,
ParserException {
FileInputStream fin = new FileInputStream(
"files/Recuring_Calendar_Entry_From_To.ics");
CalendarBuilder builder = new CalendarBuilder();
calendar = builder.build(fin);
registry = builder.getRegistry();
}
}
现在我正确地得到了简单的事件,但是由于开始和结束时间错误,一天事件发生了 3 次,发生了 4 次。
_____________
10.09.12 01:00
_____________
11.09.12 01:00
Start: 11.09.12 02:00 End: 12.09.12 02:00 Summary: Day Event 3 Times
Start: 11.09.12 10:00 End: 11.09.12 14:00 Summary: 4 hours event
_____________
12.09.12 01:00
Start: 11.09.12 02:00 End: 12.09.12 02:00 Summary: Day Event 3 Times
_____________
13.09.12 01:00
Start: 11.09.12 02:00 End: 12.09.12 02:00 Summary: Day Event 3 Times
_____________
14.09.12 01:00
Start: 11.09.12 02:00 End: 12.09.12 02:00 Summary: Day Event 3 Times
因此,如果有人知道我必须在导入或过滤器中更改什么,我会在同一时区获得所有事件。
任何建议的坦克,Cy
我做了一些测试来弄清楚发生了什么。开始是对过滤器的测试:
test/net/fortuna/ical4j/filter/PeriodRuleTest.java
如果我创建一个新的全天活动,就像它显示的那样,一切都很好。如果我添加一个 recur 规则,那么功能就会被破坏。
Recur recur = new Recur("FREQ=DAILY;COUNT=3;INTERVAL=1");
event.getProperties().add(new RRule(recur));
现在该事件匹配开始日期和接下来的 3 天,如上面的示例所示。如果我在规则中设置 COUNT=1,则事件现在匹配 2 次。也许我不明白重复规则?如果我查看导出此事件的网络日历(Zimbra),一切都像我预期的那样。