Problem of reading the date from call log

Put your problem here if it does not fit any of the other categories.

Problem of reading the date from call log

Postby demonliang » Sun May 10, 2009 5:04 am

Hi, while i reading the date from call log by the following code, i got the error date value.

All results i got are 1970-Jan-xx and so on.

I have search it in adroiddev and google, and read the SDK reference, but i just cannot find the reason.


// Querying for a cursor is like querying for any SQL-Database
ContentResolver con = this.getContentResolver();
Cursor c = con.query(android.provider.CallLog.Calls.CONTENT_URI,
null, null, null, null);

startManagingCursor(c);

int DurCol = c.getColumnIndex(
android.provider.CallLog.Calls.DURATION);
int DateCol = c.getColumnIndex(
android.provider.CallLog.Calls.DATE);

long duration, sumTotal=0, sumMonth=0, sumWeek=0, feeDur=0;
int date;
long now = System.currentTimeMillis();
Date curDate = new Date(now);

Date callDate;
String s = "";
if (c.moveToFirst()) {
do {
duration = c.getInt(DurCol);
date = c.getInt(DateCol);
Date nDate = new Date(date);
Calendar cal = Calendar.getInstance();
cal.setTime(nDate);

int iMonth = cal.get(Calendar.MONTH) + 1;
int iDay = cal.get(Calendar.DAY_OF_MONTH);
int iDayName = cal.get(Calendar.DAY_OF_WEEK);
int iHour = cal.get(Calendar.HOUR);
int iMin = cal.get(Calendar.MINUTE);
int iAmPm = cal.get(Calendar.AM_PM);

........
demonliang
Freshman
Freshman
 
Posts: 4
Joined: Sun May 10, 2009 4:57 am

Top

Postby nmc » Sun May 10, 2009 8:56 am

if 'DateCol' is in Unixtime-format (seconds since 1970-01-01), use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. long secs = c.getInt(DateCol);
  3.  
  4. Calendar date = Calendar.getInstance();
  5.  
  6. date.setTimeInMillis(secs * 1000);
  7.  
  8.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests