本文介绍了短信收件箱中的日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在短信收件箱中将此代码用于日期,但是它显示 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(); }更多推荐
短信收件箱中的日期格式
发布评论