2011-07-06 83 views
3

可能重複:
Android - get date time from SMS timestamp in miliseconds剛開日期

我使用下面的代碼來記錄短信:

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 

在此先感謝。

+2

http://stackoverflow.com/questions/4539490/android-get-date-time-from-sms-timestamp-in-miliseconds – Jacob

回答

8

嘗試使用這一段代碼:

public static String millisToDate(long currentTime) { 
    String finalDate; 
    Calendar calendar = Calendar.getInstance(); 
    calendar.setTimeInMillis(currentTime); 
    Date date = calendar.getTime(); 
    finalDate = date.toString(); 
    return finalDate; 
}