I am collecting dates of calls from the Calllog, but there is a problem with the dates. I used simpledateformat to format the numbers, but i get false data: years from 1903 to 1948 and from 1954-2036. This is the code I am using:
final Context context = getApplicationContext();
final String[] projection = null;
final String selection = android.provider.CallLog.Calls.NUMBER + "='+3620455351684'";
final String[] selectionArgs = null;
final String sortOrder = android.provider.CallLog.Calls.DATE + " DESC";
Cursor c = null;
try{
c = context.getContentResolver().query(
android.provider.CallLog.Calls.CONTENT_URI, null, selection, null, sortOrder);
while (c.moveToNext()) {
String callLogID = c.getString(c.getColumnIndex(android.provider.CallLog.Calls._ID));
int numberColumn = c.getColumnIndex(android.provider.CallLog.Calls.NUMBER);
int dateColumn = c.getColumnIndex(android.provider.CallLog.Calls.DATE);
int typeColumn = c.getColumnIndex(android.provider.CallLog.Calls.TYPE);
int outgoingtypeColumn = c.getColumnIndex(android.provider.CallLog.Calls.TYPE + "='2'");
int durationColumn = c.getColumnIndex(android.provider.CallLog.Calls.DURATION);
int person = c.getColumnIndex(android.provider.CallLog.Calls.CACHED_NAME);
int duration = c.getInt(durationColumn);
int callDate = c.getInt(dateColumn);
int callType = c.getInt(typeColumn);
String number = c.getString(numberColumn);
String personname = c.getString(person);
SimpleDateFormat datePattern = new SimpleDateFormat ("yyyy-MM-dd");;
datePattern.setTimeZone(TimeZone.getTimeZone("GMT"));
String date_str = datePattern.format(new Date(callDate*1000L));
arr_allcallsduration.add(Integer.toString(duration));
arr_calls.add(Integer.toString(duration) + " : " + number + " : " + date_str);
}
}catch(Exception ex){
}finally{
c.close();
}
Output:
56 : +3620455351684 : 1948-08-02
425 : +3620455351684 : 1947-06-17
337 : +3620455351684 : 1947-06-13
etc.
I found the solution. The callDate should be declared as long:
long callDate = c.getLong(dateColumn);
One other modification:
String date_str = datePattern.format(new Date(callDate));
Hope this helps others!