如何从Android收件箱短信转换日期格式

时间:2012-11-15 12:01:00

标签: android date inbox

我使用了这段代码:

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”
如何在字符串类型中转换为正常的日期/时间格式?

2 个答案:

答案 0 :(得分:16)

试试这个:

Date date = new Date(cursor1.getLong(0));
String formattedDate = new SimpleDateFormat("MM/dd/yyyy").format(date);

答案 1 :(得分:9)

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

long milliSeconds = cursor1.getString(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());