2012-11-15 40 views
4

我用這個代碼轉換:如何日期格式從Android的收件箱中的短信

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」
如何轉換成普通的日期/時間格式在字符串類型?

回答

15

試試這個:

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

很好用,謝謝! – user1824542

6

看來您收到以毫秒爲單位的日期。將毫秒轉換爲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());