我知道我迟到了,但只是想为任何提出这个问题的人提供iCal4j的解决方案。
实际上,时区在 iCalendar 标准中并未隐式管理,您必须VTIMEZONE
在 ICS 文件 ( RFC ) 中明确添加定义。您不能只使用时区标识符 ( TZID
)。
您可以参考这个答案来了解为什么需要时区信息。
所以这里是一个使用iCal4j的例子:
// get timezone
final TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
final TimeZone timezone = registry.getTimeZone("Europe/Zurich");
final VTimeZone tz = timezone.getVTimeZone();
// event start date
final java.util.Calendar startDate = new GregorianCalendar();
startDate.setTimeZone(timezone);
startDate.set(2016, java.util.Calendar.MARCH, 8, 9, 0, 0);
// event end date
final java.util.Calendar endDate = new GregorianCalendar();
endDate.setTimeZone(timezone);
endDate.set(2016, java.util.Calendar.MARCH, 8, 17, 0, 0);
// create event
final DateTime start = new DateTime(startDate.getTime(), timezone);
final DateTime end = new DateTime(endDate.getTime(), timezone);
final VEvent meeting = new VEvent(start, end, "Test");
meeting.getProperties().add(new RandomUidGenerator().generateUid());
meeting.getProperties().add(new Location("Somewhere"));
// create calendar
final net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
icsCalendar.getProperties().add(CalScale.GREGORIAN);
icsCalendar.getProperties().add(Version.VERSION_2_0);
// add the timezone definition <- this is what you are missing
icsCalendar.getComponents().add(tz);
// add event to calendar
icsCalendar.getComponents().add(meeting);
将VTimeZone
组件添加到Calendar
. 因此,您不需要从 Web 服务中获取定义。
您可以参考iCal4j 文档。
然后你应该最终得到这样的东西(对于Europe/Zurich
时区):
BEGIN:VCALENDAR
PRODID:-//Events Calendar//iCal4j 1.0//EN
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:20160307T231014Z
DTSTART;TZID=Europe/Zurich:20160308T090000
DTEND;TZID=Europe/Zurich:20160308T170000
SUMMARY:Test
TZID:Europe/Zurich
UID:45c269ed-dbc0-435e-bb3d-ba152dfbf0db
LOCATION:Somewhere
END:VEVENT
BEGIN:VTIMEZONE
TZID:Europe/Zurich
LAST-MODIFIED:20201011T015911Z
TZURL:http://tzurl.org/zoneinfo/Europe/Zurich
X-LIC-LOCATION:Europe/Zurich
X-PROLEPTIC-TZNAME:LMT
BEGIN:STANDARD
TZNAME:BMT
TZOFFSETFROM:+003408
TZOFFSETTO:+002946
DTSTART:18530716T000000
END:STANDARD
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+002946
TZOFFSETTO:+0100
DTSTART:18940601T000000
END:STANDARD
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19410505T010000
RRULE:FREQ=YEARLY;UNTIL=19420504T000000Z;BYMONTH=5;BYDAY=1MO
END:DAYLIGHT
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19411006T020000
RRULE:FREQ=YEARLY;UNTIL=19421005T000000Z;BYMONTH=10;BYDAY=1MO
END:STANDARD
BEGIN:DAYLIGHT
TZNAME:CEST
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
DTSTART:19810329T020000
RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU
END:DAYLIGHT
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19810927T030000
RRULE:FREQ=YEARLY;UNTIL=19950924T010000Z;BYMONTH=9;BYDAY=-1SU
END:STANDARD
BEGIN:STANDARD
TZNAME:CET
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
DTSTART:19961027T030000
RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU
END:STANDARD
END:VTIMEZONE
END:VCALENDAR