I would like my app to insert all day events into the calendar. I started off with the example given at https://developer.android.com/guide/topics/providers/calendar-provider.html. To make it all-day, I add the
long calID = 3;
long startMillis = 0;
long endMillis = 0;
Calendar beginTime = Calendar.getInstance();
beginTime.set(2012, 9, 14, 7, 30);
startMillis = beginTime.getTimeInMillis();
Calendar endTime = Calendar.getInstance();
endTime.set(2012, 9, 14, 8, 45);
endMillis = endTime.getTimeInMillis();
ContentResolver cr = getContentResolver();
ContentValues values = new ContentValues();
values.put(Events.DESCRIPTION, "Group workout");
Uri uri = cr.insert(Events.CONTENT_URI, values);
java.lang.IllegalArgumentException: DTEND and DURATION cannot both be null for an event.
DTEND is not redundant as it specifies the end of the event. all-day doesn't mean that it's just one day long. It could span any number of days. all-day means start and end of the event have no time component. In particular, the event always begins when the day begins, no matter in which time zone you're actually located at that time. A common example is your birthday. You usually celebrate it on a specific calendar day, no matter where exactly you are.
Note that in Android's calendar database
DTEND should represent midnight of that specific day in UTC.
So a better way of deriving the start and end dates would be:
TimeZone utc = TimeZone.getTimeZone("UTC"); Calendar beginTime = Calendar.getInstance(utc); // set all values to 0 beginTime.clear(); beginTime.set(2012, 9, 14); startMillis = beginTime.getTimeInMillis(); Calendar endTime = Calendar.getInstance(utc); // the event ends right before the next day begins endTime.clear(); endTime.set(2012, 9, 15); endMillis = endTime.getTimeInMillis();
Also note that
DTEND is non-inclusive (it points to the first moment after the event). For a one day all-day event that means,
DTEND is the start of the next day.
Another way of determining the end of the event is by adding the duration of the event to
DTSTART like so:
// Add the duration of 1 day to startMillis to get the end endMillis = startMillis + TimeUnit.DAYS.toMillis(1);
The result is the same as above.
Be aware that this is how to create non-recurring events. If you create a recurring event you must not set