【Java】iCalendar ライブラリ ~ biweekly ~

■ iCalendar

イカレンダ
 * スケジュールの標準フォーマット

仕様

 * RFC 5545(旧版 RFC 2445)で規定
 * 以下のサイトが詳しい
http://www.asahi-net.or.jp/~CI5M-NMR/iCal/ref.html
https://www.kanzaki.com/docs/sw/rdf-calendar.html

■ biweekly

「バイウィークリィ」。日本語で「隔週」
 * iCalendar 形式を扱うためのJavaライブラリ

公式サイト

https://github.com/mangstadt/biweekly

要件

 * Java 1.5 以上

■ 設定

 * Gradle で行う

build.gradle

dependencies {
    // biweekly
    compile 'net.sf.biweekly:biweekly:0.6.2'
}

■ サンプル

【1】アイカレンダの読み込み
【2】ファイルに書き出す
【3】アイカレンダ形式の検証

Main.java

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import biweekly.Biweekly;
import biweekly.ICalVersion;
import biweekly.ICalendar;
import biweekly.ValidationWarnings;
import biweekly.component.VEvent;
import biweekly.io.chain.ChainingTextStringParser;
import biweekly.io.chain.ChainingTextWriter;
import biweekly.property.DateStart;
import biweekly.property.Summary;
import biweekly.util.Duration;
import biweekly.util.Frequency;
import biweekly.util.Recurrence;

public class Main {

  public static void main(String[] args) {
    // 【1】アイカレンダの読み込み
    String sampleData =
        "BEGIN:VCALENDAR\r\n" +
          "VERSION:2.0\r\n" +
          "PRODID:-//Microsoft Corporation//Outlook 14.0 MIMEDIR//EN\r\n" +
          "BEGIN:VEVENT\r\n" +
            "UID:0123\r\n" +
            "DTSTAMP:20130601T080000Z\r\n" +
            "SUMMARY;LANGUAGE=jp:ハローワールド\r\n" +
            "DTSTART:20130610T120000Z\r\n" +
            "RRULE:FREQ=WEEKLY;INTERVAL=2\r\n" +
          "END:VEVENT\r\n" +
        "END:VCALENDAR\r\n";
    
    ChainingTextStringParser parser = Biweekly.parse(sampleData);
    for (ICalendar ical : parser.all()) {
      for (VEvent event : ical.getEvents()) {
        String summary = event.getSummary().getValue();
        System.out.println("summary = " + summary);
      }
    }
    
    // 【2】 ファイルに書き出す
    try {
      ICalendar ical = new ICalendar();
      VEvent event = new VEvent();
      Summary summary = event.setSummary("ハローワールド!!");
      summary.setLanguage("jp");

      Date start = new Date();
      event.setDateStart(new DateStart(start, false));
      event.setDuration(new Duration.Builder().days(1).build());
      Recurrence recurrence = new Recurrence.Builder(Frequency.YEARLY).build();
      event.setRecurrenceRule(recurrence);
      ical.addEvent(event);

      File file = new File("helloworld.ics");
      ChainingTextWriter writer = Biweekly.write(ical);
      writer.go(file);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    
    // 【3】アイカレンダ形式の検証
    try {
      DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

      ICalendar ical = new ICalendar();
      VEvent event = new VEvent();
      // 終了「2018-05-15 11:00」より開始「2018-05-15 13:00」の方が後にしている
      event.setDateStart(dateFormat.parse("2018-05-15 13:00"));
      event.setDateEnd(dateFormat.parse("2018-05-15 11:00"));
      ical.addEvent(event);
      ValidationWarnings warnings = ical.validate(ICalVersion.V2_0);
      
      System.out.println("Case1");
      System.out.println(warnings.toString());
      
      // 正しいデータ
      ICalendar icalCase2 = new ICalendar();
      VEvent eventCase2 = new VEvent();
      eventCase2.setDateStart(dateFormat.parse("2018-05-15 13:00"));
      eventCase2.setDateEnd(dateFormat.parse("2018-05-15 15:00"));
      icalCase2.addEvent(eventCase2);
      ValidationWarnings warningsCase2 = icalCase2.validate(ICalVersion.V2_0);
      
      System.out.println("Case2");
      System.out.println(warningsCase2.toString());
    } catch (Exception ex) {
      ex.printStackTrace();
    }
    
    System.out.println("Done!!");
  }
}

出力結果

コンソール
summary = ハローワールド
Case1
[ICalendar > VEvent]: (16) The start date must come before the end date.
Case2

Done!!
helloworld.ics
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Michael Angstadt//biweekly 0.6.2//EN
BEGIN:VEVENT
UID:58b66720-bf95-40d3-a4e4-da99306717b9
DTSTAMP:20180508T125230Z
SUMMARY;LANGUAGE=jp:ハローワールド!!
DTSTART;VALUE=DATE:20180508
DURATION:P1D
RRULE:FREQ=YEARLY
END:VEVENT
END:VCALENDAR


関連記事

JavaGoogle/Outlook Calendar の iCal形式のURLからデータを取得する

https://blogs.yahoo.co.jp/dk521123/37536957.html