How to request calendar events in Android for a specific date range?

I am querying an event table in android to receive events.

String[] projection = new String[] { "calendar_id", "title", "description", "dtstart", "dtend", "eventLocation" }; Cursor cursor = cr.query(Uri.parse("content://com.android.calendar/events"), projection, null, null, null); 

This returns all events. But now I want events to be held only for a certain duration: 03/03/2013 - 03/03/2013. How to use selection and selectionArgs[] ? I tried this

 String selection = "((dtstart <= ?) AND (dtend >= ?))"; String[] selectionArgs = new String[] {startString, endString}; 

But he does not return anything. My doubts 1. Will there be work with the proposal? Since startDate and endDate are first converted to long (milliseconds), then to String, and then stored in the table. So, how can strings be compared for their long values. There is no toNumber() function in sqlite. 2. If I go through selectionArgs , then startString and endString should be in what format?

+6
source share
1 answer

startString and endString must be passed in milliseconds. Try using:

 Calendar c_start= Calendar.getInstance(); c_start.set(2013,2,4,0,0); //Note that months start from 0 (January) Calendar c_end= Calendar.getInstance(); c_end.set(2013,2,7,0,0); //Note that months start from 0 (January) 

Also, I think your logic is canceled, the date range should be like this:

 String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))"; 
+7
source

All Articles