8

我使用了这段代码:

String[] columnDate =   new String[] {"date"};
Cursor cursor1 = getContentResolver().query(Uri.parse("content://sms/inbox"),
    columnDate ,null, null, "date desc limit 1");
cursor1.moveToPosition(0);
String msgData1Date = cursor1.getString(0);

..它可以工作,但以这种格式“1352933381889”给出日期
如何转换为字符串类型的正常日期/时间格式?

4

2 回答 2

16

试试这个:

Date date = new Date(cursor1.getLong(0));
String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);
于 2012-11-15T12:03:17.640 回答
9

您似乎以毫秒为单位收到日期。要将毫秒转换为 Date 对象:

long milliSeconds = cursor1.getLong(0);
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss.SSS");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
String finalDateString = formatter.format(calendar.getTime());
于 2012-11-15T12:09:38.967 回答