May 26, 2009
iCal4j にチャレンジ
iCal4j とは
iCal4j は iCalendar を Java で扱うライブラリ。 公式サイトによると
iCal4j is a Java API that provides support for the iCalendar specification as defined in RFC2445. This support includes a Parser, Object Model and Generator for iCalendar data streams.というものらしい。
- iCal4j
- http://ical4j.sourceforge.net/
- iCal4j - API Documents
- http://m2.modularity.net.au/projects/ical4j/apidocs/
- iCal4j - Download
- http://sourceforge.net/project/showfiles.php?group_id=107024
- iCal4j - Source Repository
- http://ical4j.cvs.sourceforge.net/viewvc/ical4j/iCal4j/
Maven2 での iCal4j の利用
iCal4j は Maven Central Repository に登録されている。便利。場所は こちら。 Maven2 で管理しているプロジェクトであれば pom.xml に
<project>
<dependencies>
<dependency>
<groupId>ical4j</groupId>
<artifactId>ical4j</artifactId>
<version>0.9.20</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
を追記するだけで利用出来るようになる。
サンプルコード
イベントを作成し、文字列として書き出すコードのサンプルはこちら。
// イベントの生成
VEvent event = new VEvent();
// UID
event.getProperties().add(new Uid("id"));
// タイムゾーン
TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance()
.createRegistry();
net.fortuna.ical4j.model.TimeZone timezoneAsIcalTimeZone = registry
.getTimeZone(this.timeZone.getValue());
VTimeZone tz = timezoneAsIcalTimeZone.getVTimeZone();
event.getProperties().add(tz.getTimeZoneId());
// サマリ
event.getProperties().add(new Summary("サマリ"));
// 詳細
event.getProperties().add(new Description("詳細"));
// 場所
event.getProperties().add(new Location("場所"));
// 最終更新日時
event.getProperties().add(
new LastModified(new DateTime(System.currentTimeMillis())));
// 開始日時
event.getProperties().add(
new DtStart(new DateTime(System.currentTimeMillis())));
// 終了日時
event.getProperties().add(
new DtEnd(new DateTime(System.currentTimeMillis() + 60 * 60 * 1000)));
// カレンダの生成
Calendar calendar = new Calendar();
calendar.getProperties().add(new ProdId("productId"));
calendar.getProperties().add(CalScale.GREGORIAN);
calendar.getProperties().add(Version.VERSION_2_0);
// カレンダにイベントを追加
calendar.getComponents().add(event);
// カレンダを文字列として書き出す
ByteArrayOutputStream baos = new ByteArrayOutputStream();
CalendarOutputter outputter = new CalendarOutputter();
outputter.output(calendar, baos);
String calendarAsString = baos.toString("UTF-8");
公式サイトに様々なサンプルコードが紹介されている。
- iCal4j - Examples
- http://wiki.modularity.net.au/ical4j/index.php?title=Examples
TrackBack ping me at
http://www.in-vitro.jp/blog/index.cgi/Library/20090526_01.trackback
writeback message: Ready to post a comment.
