短信收件箱中的日期格式 [英] date format in sms inbox

查看:125
本文介绍了短信收件箱中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在短信收件箱中将此代码用于日期,但是它显示 01/01/70 所有短信的错误日期如何更改正确?

I use this code for date in sms inbox but it shows 01/01/70 wrong date for all sms how do I change correct?

public void refreshSmsInbox() {
    ContentResolver contentResolver = getActivity().getContentResolver();
    Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);

    int indexBody = smsInboxCursor.getColumnIndex("body");
    int indexAddress = smsInboxCursor.getColumnIndex("address");
    int timeMillis = smsInboxCursor.getColumnIndex("date");
    Date date = new Date(timeMillis);
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
    String dateText = format.format(date);

    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;
    arrayAdapter.clear();
    do {
        String str = smsInboxCursor.getString(indexAddress) +"  "+
                "\n" + smsInboxCursor.getString(indexBody) +"\n"+ dateText+"\n";
        arrayAdapter.add(str);
    } while (smsInboxCursor.moveToNext());
    smsInboxCursor.close();
}


推荐答案

@ Mike M的评论是正确的。您正在尝试将日期列的索引转换为日期格式。你实际上并没有转换日期的值。尝试这样:

@Mike M's comment was correct. You're trying to convert the index of the date column to Date format. You're not actually converting the value of the date. Try this:

public void refreshSmsInbox() {

    ContentResolver contentResolver = getContentResolver();
    Cursor smsInboxCursor = contentResolver.query(Uri.parse("content://sms/inbox"), null, null, null, null);

    // get the index of the column
    int indexBody = smsInboxCursor.getColumnIndex("body");
    int indexAddress = smsInboxCursor.getColumnIndex("address");
    int indexDate = smsInboxCursor.getColumnIndex("date");

    if (indexBody < 0 || !smsInboxCursor.moveToFirst()) return;

    // loop through the messages in inbox
    do {
        // get the value based on the index of the column
        String address = smsInboxCursor.getString(indexAddress);
        String body = smsInboxCursor.getString(indexBody);
        long date = smsInboxCursor.getLong(indexDate);

        // convert millis value to proper format
        Date dateVal = new Date(date);
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
        String dateText = format.format(dateVal);

        String str = address + "\n" + body + "\n" + dateText + "\n";
        System.out.println(str);

    } while (smsInboxCursor.moveToNext());

    smsInboxCursor.close();
}

这篇关于短信收件箱中的日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆