Android date from calllog results in strange form

人盡茶涼 提交于 2019-12-12 06:28:55

问题


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.


回答1:


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!



来源:https://stackoverflow.com/questions/7673036/android-date-from-calllog-results-in-strange-form

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!