top / index / prev / next / target / source

2016-01-10 diary: [Java] iCal4J の利用サンプル

いがぴょんの日記 日記形式でつづる いがぴょんコラム ウェブページです。

[Java] iCal4J の利用サンプル

IgapyonICalLib.java

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.DateTime;
import net.fortuna.ical4j.model.TimeZoneRegistry;
import net.fortuna.ical4j.model.TimeZoneRegistryFactory;
import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.component.VEvent;
import net.fortuna.ical4j.model.component.VTimeZone;
import net.fortuna.ical4j.model.property.CalScale;
import net.fortuna.ical4j.model.property.Description;
import net.fortuna.ical4j.model.property.DtEnd;
import net.fortuna.ical4j.model.property.DtStart;
import net.fortuna.ical4j.model.property.LastModified;
import net.fortuna.ical4j.model.property.Location;
import net.fortuna.ical4j.model.property.Organizer;
import net.fortuna.ical4j.model.property.ProdId;
import net.fortuna.ical4j.model.property.Summary;
import net.fortuna.ical4j.model.property.Uid;
import net.fortuna.ical4j.model.property.Version;

public class IgapyonICalLib {
    /**
     * TODO replace as your product id.
     */
    public static final String PROD_ID = "-//Toshiki Iga//Simple iCal Library 1.0//EN";

    /**
     * TODO change timezone if you needed.
     */
    public static final String TIMEZONE = "Asia/Tokyo";

    /**
     * CAUTION: This sample depend on global unique value.
     */
    private long uidMillis = System.currentTimeMillis();
    private long uidSeq = 0;

    /**
     * Create Event.
     * 
     * @param emailaddr
     *            like "igapyon@igapyon.com"
     * @param summary
     * @param description
     * @param location
     * @param dateStart
     * @param dateEnd
     * @return
     */
    public VEvent createEvent(final String emailaddr, final String summary, final String description,
            final String location, final java.util.Date dateStart, final java.util.Date dateEnd) {
        final VEvent event = new VEvent();
        event.getProperties().add(new Uid("" + uidMillis + "-seq" + (uidSeq++) + "-" + emailaddr));

        final TimeZoneRegistry tzReg = TimeZoneRegistryFactory.getInstance().createRegistry();
        net.fortuna.ical4j.model.TimeZone icalTimeZone = tzReg.getTimeZone(TIMEZONE);
        final VTimeZone vtz = icalTimeZone.getVTimeZone();
        event.getProperties().add(vtz.getTimeZoneId());
        event.getProperties().add(new Summary(summary));
        event.getProperties().add(new Description(description));
        event.getProperties().add(new Location(location));
        event.getProperties().add(new LastModified(new DateTime(System.currentTimeMillis())));

        try {
            event.getProperties().add(new Organizer("MAILTO:" + emailaddr));
        } catch (URISyntaxException ex) {
            System.out.println(ex.toString());
        }

        event.getProperties().add(new DtStart(new DateTime(dateStart.getTime())));
        event.getProperties().add(new DtEnd(new DateTime(dateEnd.getTime())));

        return event;
    }

    /**
     * VEvent to iCal String.
     * 
     * @param event
     * @return
     * @throws IOException
     */
    public static String vevent2CalString(final VEvent event) throws IOException {
        net.fortuna.ical4j.model.Calendar icalCalendar = new net.fortuna.ical4j.model.Calendar();
        icalCalendar.getProperties().add(new ProdId(PROD_ID));
        icalCalendar.getProperties().add(CalScale.GREGORIAN);
        icalCalendar.getProperties().add(Version.VERSION_2_0);

        // Treat as single event calendar.
        icalCalendar.getComponents().add(event);

        // ical to string.
        final ByteArrayOutputStream bytearrayOutputStream = new ByteArrayOutputStream();
        try {
            new CalendarOutputter().output(icalCalendar, bytearrayOutputStream);
        } catch (ValidationException ex) {
            throw new IOException(ex);
        }

        return bytearrayOutputStream.toString("UTF-8");
    }

    /**
     * new Date
     * 
     * @param year
     * @param month
     * @param date
     * @param hour
     * @param minute
     * @return
     */
    public static java.util.Date newDate(final int year, final int month, final int date, final int hour,
            final int minute) {
        final java.util.Calendar cal = java.util.Calendar.getInstance();
        cal.clear();
        cal.set(java.util.Calendar.YEAR, year);
        cal.set(java.util.Calendar.MONTH, month - 1);
        cal.set(java.util.Calendar.DAY_OF_MONTH, date);
        cal.set(java.util.Calendar.HOUR_OF_DAY, hour);
        cal.set(java.util.Calendar.MINUTE, minute);
        return cal.getTime();
    }
}

それの呼び出し元

import java.io.IOException;

import net.fortuna.ical4j.model.ValidationException;
import net.fortuna.ical4j.model.component.VEvent;

public class MyICalSample {

    public static void main(final String[] args) throws IOException, ValidationException {
        final IgapyonICalLib icallib = new IgapyonICalLib();

        final VEvent event = icallib.createEvent("igapyon@igapyon.com", "弦楽四重奏の練習#1", "アメリカ第一楽章を練習", "いつもの練習場所",
                IgapyonICalLib.newDate(2015, 12, 31, 17, 30), IgapyonICalLib.newDate(2015, 12, 31, 19, 30));
        final String calendarAsString = IgapyonICalLib.vevent2CalString(event);
        System.out.println(calendarAsString);
    }
}

実行結果

BEGIN:VCALENDAR
PRODID:-//Toshiki Iga//Simple iCal Library 1.0//EN
CALSCALE:GREGORIAN
VERSION:2.0
BEGIN:VEVENT
DTSTAMP:20160114T081513Z
UID:1452759313289-seq0-igapyon@igapyon.com
TZID:Asia/Tokyo
SUMMARY:弦楽四重奏の練習#1
DESCRIPTION:アメリカ第一楽章を練習
LOCATION:いつもの練習場所
LAST-MODIFIED:20160110T081513Z
ORGANIZER:MAILTO:igapyon@igapyon.com
DTSTART:20151231T173000
DTEND:20151231T193000
END:VEVENT
END:VCALENDAR

登場キーワード


この日記について