Calendar Toolbox API

包结构概览

库的代码组织在基础包 com.onixbyte.calendar 下,结构如下:

描述
com.onixbyte.calendar根日历对象
com.onixbyte.calendar.componentiCalendar 组件(VEVENT、VTODO 等)
com.onixbyte.calendar.component.property组件级属性
com.onixbyte.calendar.property日历级属性
com.onixbyte.calendar.parameteriCalendar 属性参数
com.onixbyte.calendar.recurrence重复规则类型
com.onixbyte.calendar.value值类型
com.onixbyte.calendar.util格式化工具类

Calendar

表示 iCalendar(RFC 5545)VCALENDAR 容器的顶层对象。

Calendar calendar = Calendar.builder()
    .withCalendarScale(...)
    .withMethod(...)
    .withProductIdentifier(...)
    .withVersion(...)
    .withOwner(...)
    .withPrimaryCalendar(...)
    .withPublishedTTL(...)
    .withCalendarDescription(...)
    .withCalendarName(...)
    .withCalendarId(...)
    .withCustomProperties(...)
    .withComponents(event, todo, ...)
    .build();

String icsContent = calendar.formatted();

日历属性

所有日历级属性位于 com.onixbyte.calendar.property,实现 CalendarProperty 接口。

iCalendar 属性描述
CalendarScaleCALSCALE日历系统(例如 GREGORIAN)
MethodMETHODiCalendar 方法(例如 PUBLISH、REQUEST)
ProductIdentifierPRODID日历的产品标识符
VersionVERSIONiCalendar 版本(例如 2.0)
OwnerX-OWNER日历所有者
PrimaryCalendarX-PRIMARY-CALENDAR是否为主日历
PublishedTTLX-PUBLISHED-TTL已发布日历的生存时间
CalendarDescriptionX-CALENDAR-DESC日历描述
CalendarNameX-CALENDAR-NAME日历名称
CalendarIdX-CALENDAR-ID日历标识符
CustomCalendarPropertyX-*自定义日历属性

自定义属性使用 Builder 创建:

var customProp = CustomCalendarProperty.builder()
    .withParameters(...)
    .build("X-MY-PROPERTY", "my-value");

注意:自定义属性名称必须X- 开头。


组件

组件是 iCalendar 对象的核心实体。每个组件实现 CalendarComponent 接口,并在 BEGIN:TYPEEND:TYPE 定界符之间渲染。

Event(VEVENT)

表示一个预定的活动/事件。

Event event = Event.builder()
    .withDateTimeStamp(dtstamp)        // 必需
    .withUniqueIdentifier(uid)         // 必需
    .withDateTimeStart(dtstart)
    .withDateTimeEnd(dtend)            // 与 duration 互斥
    .withDuration(duration)            // 与 dtend 互斥
    .withSummary(summary)
    .withDescription(description)
    .withClassification(classification)
    .withDateTimeCreated(dateTimeCreated)
    .withGeographicPosition(geoPos)
    .withLastModified(lastModified)
    .withLocation(location)
    .withOrganiser(organiser)
    .withPriority(priority)
    .withSequenceNumber(seqNumber)
    .withStatus(status)
    .withTimeTransparency(transparency)
    .withUniformResourceLocator(url)
    .withRecurrenceId(recurrenceId)
    .withRecurrenceRule(rrule)
    .withAttachments(attachments)
    .withAttendees(attendees)
    .withCategories(categories)
    .withComments(comments)
    .withContacts(contacts)
    .withExceptionDateTimes(exDates)
    .withRequestStatuses(statuses)
    .withRelated(related)
    .withResources(resources)
    .withRecurrenceDateTimes(rDates)
    .build();

Todo(VTODO)

表示待办事项或任务。

Todo todo = Todo.builder()
    .withDateTimeStamp(dtstamp)        // 必需
    .withUniqueIdentifier(uid)         // 必需
    .withSummary(summary)
    .withDateTimeStart(dtstart)
    .withDateTimeDue(due)              // 与 duration 互斥
    .withDuration(duration)            // 与 due 互斥
    .withDateTimeCompleted(completed)
    .withPercentComplete(percent)
    .withClassification(classification)
    .withDateTimeCreated(created)
    .withDescription(description)
    .withGeographicPosition(geoPos)
    .withLastModified(lastModified)
    .withLocation(location)
    .withOrganiser(organiser)
    .withPriority(priority)
    .withSequenceNumber(seq)
    .withStatus(status)
    .withRecurrenceId(recurId)
    .withRecurrenceRule(rrule)
    .withUniformResourceLocator(url)
    .withAttachments(attachments)
    .withAttendees(attendees)
    .withCategories(categories)
    .withComments(comments)
    .withContacts(contacts)
    .withExceptionDateTimes(exDates)
    .withRequestStatuses(statuses)
    .withRelatedToList(related)
    .withResources(resources)
    .withRecurrenceDateTimes(rDates)
    .build();

Journal(VJOURNAL)

表示日记条目或笔记。

Journal journal = Journal.builder()
    .withDateTimeStamp(dtstamp)        // 必需
    .withUniqueIdentifier(uid)         // 必需
    .withSummary(summary)
    .withClassification(classification)
    .withDateTimeCreated(created)
    .withDateTimeStart(dtstart)
    .withLastModified(lastModified)
    .withOrganiser(organiser)
    .withRecurrenceId(recurId)
    .withSequenceNumber(seq)
    .withStatus(status)
    .withUniformResourceLocator(url)
    .withRecurrenceRule(rrule)
    .withAttachments(attachments)
    .withAttendees(attendees)
    .withCategories(categories)
    .withComments(comments)
    .withContacts(contacts)
    .withDescriptions(descriptions)
    .withExceptionDateTimes(exDates)
    .withRelatedToList(related)
    .withRecurrenceDate(rDates)
    .withRequestStatuses(statuses)
    .build();

FreeBusy(VFREEBUSY)

表示空闲/忙碌时间信息。

FreeBusy freeBusy = FreeBusy.builder()
    .withDateTimeStamp(dtstamp)        // 必需
    .withUniqueIdentifier(uid)         // 必需
    .withContact(contact)
    .withDateTimeStart(dtstart)
    .withDateTimeEnd(dtend)
    .withOrganiser(organiser)
    .withUniformResourceLocator(url)
    .withAttendees(attendees)
    .withComments(comments)
    .withFreeBusyTimes(freeBusyTimes)
    .withRequestStatuses(statuses)
    .build();

TimeZone(VTIMEZONE)

定义时区规则,包括标准时间和夏令时转换。

TimeZone timezone = TimeZone.builder()
    .withTimeZoneIdentifier(tzId)
    .withLastModified(lastModified)
    .withTimeZoneUrl(tzUrl)
    .withTimeZoneProperties(standardProp, daylightProp)
    .build();

时区属性定义实际的转换规则:

TimeZoneProperty standard = TimeZoneProperty.builder()
    .withDateTimeStart(dtstart)
    .withTimeZoneOffsetTo(offsetTo)
    .withTimeZoneOffsetFrom(offsetFrom)
    .withRecurrenceRule(rrule)
    .withTimeZoneNames(tzName)
    .buildStandard();                  // 或 buildDaylight()

TimeZoneProperty daylight = TimeZoneProperty.builder()
    .withDateTimeStart(dtstart)
    .withTimeZoneOffsetTo(offsetTo)
    .withTimeZoneOffsetFrom(offsetFrom)
    .buildDaylight();

Alarm(VALARM)

定义闹钟/提醒通知。支持三种闹钟类型:

// 音频闹钟
Alarm audioAlarm = Alarm.builder()
    .withTrigger(trigger)
    .withDuration(duration)
    .withRepeatCount(repeatCount)
    .withAttachments(audioAttachment)
    .buildAudio();

// 显示闹钟
Alarm displayAlarm = Alarm.builder()
    .withTrigger(trigger)
    .withDescription(description)      // 必需
    .withDuration(duration)
    .withRepeatCount(repeatCount)
    .buildDisplay();

// 邮件闹钟
Alarm emailAlarm = Alarm.builder()
    .withTrigger(trigger)
    .withDescription(description)      // 必需
    .withSummary(summary)              // 必需
    .withAttendees(attendees)          // 必需
    .withDuration(duration)
    .withRepeatCount(repeatCount)
    .buildEmail();

组件属性

组件属性位于 com.onixbyte.calendar.component.property,实现 ComponentProperty 接口。

iCalendar 属性描述
ActionACTION闹钟操作类型(AUDIO、DISPLAY、EMAIL)
AttachmentATTACH文档附件
AttendeeATTENDEE事件/任务参与者
CategoriesCATEGORIES类别或标签
ClassificationCLASS访问分类(PUBLIC、PRIVATE、CONFIDENTIAL)
CommentCOMMENT评论
ContactCONTACT联系信息
DateTimeCompletedCOMPLETED完成日期时间
DateTimeCreatedCREATED创建日期时间
DateTimeDueDUE截止日期时间
DateTimeEndDTEND结束日期时间
DateTimeStampDTSTAMP日期时间戳
DateTimeStartDTSTART开始日期时间
DescriptionDESCRIPTION描述
ExceptionDateTimesEXDATE例外日期时间
FreeBusyTimeFREEBUSY空闲/忙碌时间段
GeographicPositionGEO地理位置(纬度/经度)
LastModifiedLAST-MODIFIED最后修改日期时间
LocationLOCATION地点
OrganiserORGANIZER组织者
PercentCompletePERCENT-COMPLETE完成百分比
PriorityPRIORITY优先级
RecurrenceDateTimesRDATE重复日期时间
RecurrenceIdRECURRENCE-ID重复实例标识符
RecurrenceRuleRRULE重复规则
RelatedToRELATED-TO相关组件引用
RepeatCountREPEAT闹钟重复次数
RequestStatusREQUEST-STATUS请求状态
ResourcesRESOURCES资源
SequenceNumberSEQUENCE序列号(修订版本)
StatusSTATUS组件状态(TENTATIVE、CONFIRMED、CANCELLED 等)
SummarySUMMARY标题或摘要
TimeTransparencyTRANSP时间透明度(OPAQUE、TRANSPARENT)
TimeZoneIdentifierTZID时区标识符
TimeZoneNameTZNAME时区名称
TimeZoneOffsetFromTZOFFSETFROM相对于 UTC 的时区偏移(原)
TimeZoneOffsetToTZOFFSETTO相对于 UTC 的时区偏移(目标)
TimeZoneUrlTZURL时区 URL
TriggerTRIGGER闹钟触发器
UniformResourceLocatorURL关联 URL
UniqueIdentifierUID唯一标识符
CustomComponentPropertyX-*自定义组件属性

参数

参数位于 com.onixbyte.calendar.parameter,为组件属性提供额外限定。

iCalendar 参数描述
AlarmTriggerRelationshipRELATED触发器关联(START、END)
AlternateTextRepresentationALTREP替代文本 URI
CalendarUserTypeCUTYPE日历用户类型(INDIVIDUAL、GROUP、RESOURCE 等)
CommonNameCN通用名称
DelegateesDELEGATED-TO受托人
DelegatorsDELEGATED-FROM委托人
DirectoryEntryReferenceDIR目录条目 URI
FormatTypeFMTTYPE格式类型(MIME 类型)
FreeBusyTimeTypeFBTYPE空闲/忙碌时间类型
InlineEncodingENCODING内联编码(BASE64)
LanguageLANGUAGE语言
MembershipMEMBER组成员
ParticipationRoleROLE参与角色(CHAIR、REQ-PARTICIPANT 等)
ParticipationStatusPARTSTAT参与状态(ACCEPTED、DECLINED 等)
RecurrenceIdentifierRangeRANGE重复范围(THISANDPRIOR、THISANDFUTURE)
RelationshipTypeRELTYPE关系类型(PARENT、CHILD、SIBLING)
RsvpExpectationRSVPRSVP 期望(TRUE、FALSE)
SentBySENT-BY发送者
TimeZoneIdentifierTZID时区标识符
ValueDataTypeVALUE值数据类型(DATE、DATE-TIME 等)

递归类型

位于 com.onixbyte.calendar.recurrence

描述
Frequency重复频率常量(DAILY、WEEKLY、MONTHLY、YEARLY),用于重复规则
WeekdayNum重复规则的星期序号(例如第 2 个星期一)

值类型

位于 com.onixbyte.calendar.value

描述
FreeBusyTimeValue表示空闲/忙碌时间值
PeriodOfTime表示一个时间段,包含开始和结束
PropertyValue属性的基础值类型
UtcOffsetUTC 偏移值(例如 -05:00+01:00

格式化输出

所有组件和属性都提供 formatted() 方法,返回符合 iCalendar 规范的字符串。Calendar.formatted() 方法生成完整的 .ics 输出,包含 BEGIN:VCALENDAR/END:VCALENDAR

String ics = calendar.formatted();
// BEGIN:VCALENDAR
// CALSCALE:GREGORIAN
// METHOD:PUBLISH
// ...
// END:VCALENDAR