Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query android calendar events for specific date range?

I am querying events table in android to get 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 the events. But now I want events only in specific duration say 4/03/2013 to 7/03/2013. How do I use selection and selectionArgs[] ? I tried this

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

But its not returning anything. My doubt are 1. Will where clause work ? Because, startDate and endDate are first converted to long (milliseconds) and then to String and then are stored in the table. So how can strings be compared for their long values. There is no toNumber() function in sqlite. 2. If I'm passing selectionArgs then startString and endString should be in which format ?

like image 980
Chimu Avatar asked Mar 18 '13 13:03

Chimu


1 Answers

startString & endString should be passed in millis. 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 reversed, date range should be like this:

String selection = "((dtstart >= "+c_start.getTimeInMillis()+") AND (dtend <= "+c_end.getTimeInMillis()+"))";
like image 193
Mohamed_AbdAllah Avatar answered Nov 16 '22 18:11

Mohamed_AbdAllah