短信收件箱中的日期格式

编程入门 行业动态 更新时间:2024-10-20 03:24:23
本文介绍了短信收件箱中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在短信收件箱中将此代码用于日期,但是它显示 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(); }

更多推荐

短信收件箱中的日期格式

本文发布于:2023-10-12 23:44:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收件箱   日期   短信   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!