Quantcast
Channel: プログラム の個人的なメモ
Viewing all articles
Browse latest Browse all 860

【Java】Google/Outlook Calendar の iCal形式のURLからデータを取得する

$
0
0

■ はじめに

https://blogs.yahoo.co.jp/dk521123/37534228.html
だと、Google Calendar専用の処理になり、
例えば、Outlookのような他のカレンダーシステムも対応する場合は
別の処理を実装しなおさなければならない。
また、APIの仕様がいつ変更されるのか?どのように変更されるのか?などの懸念もある。

そこで、以下の関連記事で扱った標準のiCal形式をURLから取得できるので
そのサンプルを作成してみた
https://blogs.yahoo.co.jp/dk521123/37532480.html

■ サンプル

import java.io.InputStream;
import java.net.URL;

import biweekly.Biweekly;
import biweekly.ICalendar;
import biweekly.component.VEvent;
import biweekly.io.chain.ChainingTextParser;

public class GoogleCalendarDemo {
  /** Google Calendar : https://calendar.google.com/calendar/ical/【ユーザ名】%40gmail.com/public/basic.ics */
  private static final String TARGET_URL_FOR_GOOGLE_CALENDAR = "https://calendar.google.com/calendar/ical/yourusername%40gmail.com/public/basic.ics";
  /** Outlook Calendar */
  private static final String TARGET_URL_FOR_OUTLOOK_CALENDAR = "https://outlook.live.com/owa//calendar/XXXXXXXXXXXXX-XXXX-XXXXXXXXXXXXXXX/cid-XXXXXXXXXXXX/calendar.ics";
  
    
  public static void main(String[] args) {
    System.out.println("Start!");
    
    System.out.println("From Google");
    printICal(TARGET_URL_FOR_GOOGLE_CALENDAR);
    
    System.out.println("From Outlook");
    printICal(TARGET_URL_FOR_OUTLOOK_CALENDAR);
    
    System.out.println("Done!!");
  }

  private static void printICal(String targetUrl) {
    try {
      URL url = new URL(targetUrl);
      try (InputStream inputStream = url.openStream();) {
        ChainingTextParser<ChainingTextParser<?>> parser = Biweekly
            .parse(inputStream);
        for (ICalendar ical : parser.all()) {
          System.out.println("event List size = " + ical.getEvents().size());
          for (VEvent event : ical.getEvents()) {
            String summary = event.getSummary().getValue();
            System.out.println("summary = " + summary);
          }
        }
      }
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

出力結果例

Start!
From Google
event List size = 2
summary = テストのために
summary = サンプル
From Outlook
event List size = 2
summary = Sample
summary = This is a sample. Hello World!!
Done!!

関連記事

Java で Google Calendar API を使う

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

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

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

Viewing all articles
Browse latest Browse all 860

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>