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 curso
Solution 1:
String[] projection = newString[] { 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 2014String 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: " + (newDate(cursor.getLong(3))).toString(), Toast.LENGTH_LONG ).show();
} while ( cursor.moveToNext());
}
Solution 2:
This was what I implemented to retrieve the events:
if (Integer.parseInt(Build.VERSION.SDK) >= 8 || Integer.parseInt(Build.VERSION.SDK) <= 13 ) {
uri = "content://com.android.calendar/events";
CALENDAR_URI = Uri.parse(uri);
} elseif(Integer.parseInt(Build.VERSION.SDK) >= 14){
CALENDAR_URI = CalendarContract.Events.CONTENT_URI;
}
else {
uri = "content://calendar/events";
CALENDAR_URI = Uri.parse(uri);
}
Cursor cursors = context.getContentResolver().query(CALENDAR_URI, newString[]{ "_id", "title", "description", "dtstart", "dtend", "eventLocation" },
null,null, null);
cursors.moveToFirst();
String[] CalNames = newString[cursors.getCount()];
int[] CalIds = new int[cursors.getCount()];
for (int i = 0; i < CalNames.length; i++) {
CalIds[i] = cursors.getInt(0);
CalNames[i] = "Event"+cursors.getInt(0)+": \nTitle: "+ cursors.getString(1)+"\nDescription: "+cursors.getString(2)+"\nStart Date: "+newDate(cursors.getLong(3))+"\nEnd Date : "+newDate(cursors.getLong(4))+"\nLocation : "+cursors.getString(5);
Date mDate = newDate(cursors.getLong(3));
Date nDate = newDate(cursors.getLong(4));
long mTime = mDate.getTime();
long lTime = nDate.getTime();
if(stTime <= mTime && enTime >= lTime){
String eid = cursors.getString(0);
int eID = Integer.parseInt(eid);
String desc = cursors.getString(2);
String title = cursors.getString(1);
In this case your stTime will be yo dtstart and enTime will be your dtend.
Post a Comment for "How To Get Calendar Event By Date Range Or Month In Android"