How to get time of calls from android call log and convert it to DateFormat
I am trying to get calls time from call log in android sdk 2.2.
long seconds = cursor.getColumnIndex(Calls.DATE);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
St开发者_开发百科ring dateString = formatter.format(new Date(seconds * 1000L));
destinationTime.setText(dateString);
But I am getting the wrong result
01-01-70 05:29
Can someone point me to the right direction, as what I am doing wrong.
Thanks
while (Cursor_Call_Logs.moveToNext()) {
String callDate = Cursor_Call_Logs.getString(Cursor_Call_Logs.getColumnIndex(android.provider.CallLog.Calls.DATE));
long seconds=Long.parseLong(callDate);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
String dateString = formatter.format(new Date(seconds));
}
Change ur answer to
int secondindex = cursor.getColumnIndex(Calls.DATE);
long seconds=cursor.getLong(secondindex);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yy HH:mm");
String dateString = formatter.format(new Date(seconds));
destinationTime.setText(dateString);
First you should use the index to get the value. Index itself is not the value . Second to use the constructor of Date object you should give millis as argument . You converted it to seconds by * 1000L . It should work fine now .
getColumnIndex()
- that's the problem, you're getting the column index, not the actual data. It should be something like this:
long dateTimeMs = cursor.getLong(cursor.getColumnIndex(Calls.DATE));
精彩评论