【问题标题】:date format in sms inbox短信收件箱中的日期格式
【发布时间】:2016-06-14 07:31:08
【问题描述】:

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

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();
}

【问题讨论】:

  • 先回复一下谢谢
  • "date" 列的索引不是日期。
  • 评论更改
  • 看看如何获​​取地址和正文的值。

标签: android date android-fragments


【解决方案1】:

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

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();
}

【讨论】:

    【解决方案2】:

    这部分错了:

    int timeMillis = smsInboxCursor.getColumnIndex("date");
    Date date = new Date(timeMillis);
    

    getColumnIndex 返回一个索引,而不是实际值。我想你想要这个,虽然我自己没有测试过:

    int dateIndex = smsInboxCursor.getColumnIndex("date");
    long timeMillis = smsInboxCursor.getLong(dateIndex);
    Date date = new Date(timeMillis);
    

    【讨论】:

    • 不幸的是应用程序现在停止了
    • @mgcaguioa 的回答比我的要好......它还解决了您没有在实际循环中获取值的事实。
    • 帮助先生解决这个问题,可以在哪里放置此代码,它显示光标索引超出范围异常索引 -1 请求 9
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多