Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Calendar event by date range or month in android

I am using below code to get and load calendar events in my application. It's working perfectly. But now i want to get events specified date range. How can i get it..

Cursor cursor = getContentResolver().query(
                Uri.parse("content://com.android.calendar/events"),
                new String[] { "_id", "title", "dtstart", "dtend" }, null,
                null, "dtstart ASC");

should be
dtstart = "2013-01-01"
dtend= "2013-01-31"

like image 526
Ask Shiwa Avatar asked Nov 29 '22 13:11

Ask Shiwa


1 Answers

String[] projection = new String[] { CalendarContract.Events.CALENDAR_ID, CalendarContract.Events.TITLE, CalendarContract.Events.DESCRIPTION, CalendarContract.Events.DTSTART, CalendarContract.Events.DTEND, CalendarContract.Events.ALL_DAY, CalendarContract.Events.EVENT_LOCATION };

// 0 = January, 1 = February, ...

Calendar startTime = Calendar.getInstance();
startTime.set(2014,00,01,00,00);

Calendar endTime= Calendar.getInstance();
endTime.set(2015,00,01,00,00);

// the range is all data from 2014

String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + startTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ))";

Cursor cursor = this.getBaseContext().getContentResolver().query( CalendarContract.Events.CONTENT_URI, projection, selection, null, null );

// output the events 

if (cursor.moveToFirst()) {
    do {
        Toast.makeText( this.getApplicationContext(), "Title: " + cursor.getString(1) + " Start-Time: " + (new Date(cursor.getLong(3))).toString(), Toast.LENGTH_LONG ).show();
    } while ( cursor.moveToNext());
}
like image 108
Erdinc Ay Avatar answered Dec 15 '22 05:12

Erdinc Ay