kura
kura

Reputation: 45

get calendar events for one day into android app

I want to know how can i get an event from the calendar of a specific date I just wanna read the events of the selected day ?

this way read all calendar events:

while (cur.moveToNext()) {
    String title = null;
    long eventID = 0;
    long beginVal = 0;    

    // Get the field values
    eventID = cur.getLong(PROJECTION_ID_INDEX);
    beginVal = cur.getLong(PROJECTION_BEGIN_INDEX);
    title = cur.getString(PROJECTION_TITLE_INDEX);

    // Do something with the values. 
    Log.i(DEBUG_TAG, "Event:  " + title); 
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(beginVal);  
    DateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
    Log.i(DEBUG_TAG, "Date: " + formatter.format(calendar.getTime()));    
    }
 }

Upvotes: 3

Views: 3708

Answers (1)

matiash
matiash

Reputation: 55350

You should use a selection in your query to filter events by their start date. For example.

Calendar calendar = Calendar.getInstance();
calendar.set(2014, Calendar.MAY, 14, 0, 0, 0);
long startDay = calendar.getTimeInMillis();
calendar.set(2014, Calendar.MAY, 14, 23, 59, 59);
long endDay = calendar.getTimeInMillis();

String[] projection = new String[] { BaseColumns._ID, CalendarContract.Events.TITLE, CalendarContract.Events.DTSTART };
String selection = CalendarContract.Events.DTSTART + " >= ? AND " + CalendarContract.Events.DTSTART + "<= ?";
String[] selectionArgs = new String[] { Long.toString(startDay), Long.toString(endDay) }; 

Cursor cur = getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, selectionArgs, null);

Upvotes: 8

Related Questions