returns an iCalendar formatted UTC date-time ex: 20141120T120000Z for noon on Nov 20, 2014 - Android Android OS

Android examples for Android OS:Calendar Contract

Description

returns an iCalendar formatted UTC date-time ex: 20141120T120000Z for noon on Nov 20, 2014

Demo Code


import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.CalendarContract;
import com.android.calendar.CalendarEventModel;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;
import java.util.TimeZone;

public class Main{
    /**//from   ww w.  j  a v a 2  s.c  o  m
     * returns an iCalendar formatted UTC date-time
     * ex: 20141120T120000Z for noon on Nov 20, 2014
     *
     * @param millis in epoch time
     * @param timeZone indicates the time zone of the input epoch time
     * @return
     */
    public static String getICalFormattedDateTime(long millis,
            String timeZone) {
        if (millis < 0)
            return null;

        Calendar calendar = Calendar.getInstance(TimeZone
                .getTimeZone(timeZone));
        calendar.setTimeInMillis(millis);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
                "yyyyMMddHHmmss");
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
        String dateTime = simpleDateFormat.format(calendar.getTime());
        StringBuilder output = new StringBuilder(16);

        // iCal UTC date format : <yyyyMMdd>T<HHmmss>Z
        return output.append(dateTime.subSequence(0, 8)).append("T")
                .append(dateTime.substring(8)).append("Z").toString();
    }
}

Related Tutorials