2013-12-23 42 views
2

我必須在時間戳中顯示消息創建時間聊天應用程序現在我所有的時間戳它會自動顯示設備時間當我打開應用程序它不顯示消息創建時間。檢查我的代碼並改變我如果我做錯了。Android:消息創建時間不顯示在時間戳

public static String getFormatedTime(long dateTime,String timeZone, String format){ 
    String time = null; 
    try{ 
     Calendar cal = Calendar.getInstance(); 
     cal.setTimeInMillis(dateTime); 
     SimpleDateFormat sdf = new SimpleDateFormat(format); 
     sdf.setTimeZone(TimeZone.getTimeZone(timeZone)); 
     time = sdf.format(cal.getTime()); 
    } 
    catch(Exception e){ 
     //logger.log(Level.SEVERE,"\n ERROR**********, Exception during get formated time: "+e+"\n");  
    } 
    return time; 
} 

Calendar calendar = Calendar.getInstance(); 
TimeZone zone = calendar.getTimeZone(); 
String timeZone = zone.getID(); 

String msgAtTime = getFormattedTime(System.currentTimeInMillis(), timezone, "MMM dd, hh:mm a"); 

msgTextVies.setText(msgAtTime); 

我想告訴創建時間像這樣使用上面的代碼的消息:

//顯示日期時間

DateTime createdAt = message.getCreatedAt(); 

但我不知道如何使用以前來實現它碼。

回答

2

只需使用下面的代碼即可獲取消息創建時間。

public String getCreatedAt() 
{ 
    Calendar c1 = Calendar.getInstance();  
    String msg_time= c1.get(Calendar.YEAR) + c1.get(Calendar.MONTH) + c1.get(Calendar.DAY_OF_MONTH) + c1.get(Calendar.HOUR_OF_DAY) + c1.get(Calendar.MINUTE); 
    return msg_time; 
} 

我還沒有設置任何特定的格式,但您可以根據您的要求進行調整。

希望它適合你。

+0

c1.get(Calendar.MONTH)很難提供直觀的月份數(索引從零開始)。我認爲,@Android_learner首先要將月份作爲縮寫,其次是代碼組織方面的問題,因爲他已經完成了所有解決方案。 –

1

您可以通過兩種方式來完成。

1.按設備端數據庫:在存儲消息信息的設備中創建數據庫並獲取它的時間。並將該數據庫數據填充到您的應用程序中。所以你會在你的時間表中獲得所有發佈的消息時間。

2.通過服務器端數據庫:希望你打電話給web服務發送消息從一個用戶到另一個。那時候,也會傳遞設備的當前時間。或者讓系統在任何時候獲得任何條目時,還會輸入該表的當前時間戳。因此,無論您何時調用Web服務,您都會收到消息發佈時間。在List中填充那個時間。

請讓我知道,如果你沒有讓我清楚。