gpt4 book ai didi

android - 短信收件箱中的日期格式

转载 作者:行者123 更新时间:2023-11-30 01:16:22 25 4
gpt4 key购买 nike

我在短信收件箱中使用此代码作为日期,但它显示所有短信的 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();
}

最佳答案

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

关于android - 短信收件箱中的日期格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37805655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com