Search code examples
androidsqliteandroid-cursorcalllog

How to get Call Log by date in android?


I am trying to get Call Logs by date. But get stuck with the format of the date to be passed to the cursor to get the values.

Call Log Cursor

Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");

String selection = CallLog.Calls.DATE + " = "+dateFormatter.format(calendar.getTime());

Cursor mCallLogCursor = mContext.getContentResolver().query(
            CallLog.Calls.CONTENT_URI, projection, selection, null,
            CallLog.Calls._ID + " DESC");

Tried with various dateformatter type but none of them working.

What I am doing wrong here?


Solution

  • Try this:

    Cursor c = contentResolver.query(CallLog.Calls.CONTENT_URI,
                    new String[] { CallLog.Calls.DATE, CallLog.Calls.DURATION,
                            CallLog.Calls.NUMBER, CallLog.Calls._ID },
                    CallLog.Calls.DATE + ">?",
                    new String[] { String.valueOf(sinceDate.getTime())},
                    CallLog.Calls.NUMBER + " asc");
    

    sinceDate is a Date variable that is set to the beginning of the period you will want to cover.