可能重复:
Android-从短信时间戳中获取日期时间,单位为毫秒
我正在使用以下代码来记录短信:
public void getColumnData(Cursor cur, Context context) {
ArrayList<String> exportBuffer = new ArrayList<String>();
try {
if (cur.moveToFirst()) {
String id;
String date;
String phoneNumber;
String body;
int idColumn = cur.getColumnIndex("_id");
int dateColumn = cur.getColumnIndex("date");
int numberColumn = cur.getColumnIndex("address");
int bodyColumn = cur.getColumnIndex("body");
do {
id = cur.getString(idColumn);
date = cur.getString(dateColumn);
body = cur.getString(bodyColumn);
phoneNumber = cur.getString(numberColumn);
String FormattedDate;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss dd-MM-yyyy");
FormattedDate = sdf.format(date).toString();
exportBuffer.add(id + " ," + FormattedDate + " ," + body + " ,"
+ phoneNumber);
} while (cur.moveToNext());
}
WriteToFile(exportBuffer,context);
} catch (Exception e) {
int MessageDuration = Toast.LENGTH_SHORT;
CharSequence text = "An Error Occurred, Code:104";
Toast toast = Toast.makeText(context, text, MessageDuration);
toast.show();
}
}
除了日期字符串之外,一切都很好。我不知道它是什么格式,这意味着我无法将其格式化为正确的日期。以下是其中一个日期的示例:
1309817682651
提前谢谢。
尝试使用以下代码:
public static String millisToDate(long currentTime) {
String finalDate;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentTime);
Date date = calendar.getTime();
finalDate = date.toString();
return finalDate;
}