簡體   English   中英

Android:消息創建時間未在時間戳中顯示

[英]Android:Message created time is not displayed in timestamp

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

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

DateTime createdAt = message.getCreatedAt();

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

只需使用以下代碼即可獲取“消息創建時間”。

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;
}

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

希望對你有效。

您可以通過兩種方式來實現。

1.按設備端數據庫:在設備中建立數據庫,用於存儲消息信息及其獲取時間。 並將該數據庫數據填充到您的應用程序中。 因此,您將在時間軸中獲得所有發布的消息時間。

2.通過服務器端數據庫:希望您正在調用Web服務,以將消息從一個用戶發送到另一個用戶。 那時,還要傳遞設備的當前時間。 或者,要么使該系統獲得任何條目,要么也有該表當前時間戳的條目。 因此,每當您調用Web服務時,您都應該獲得消息發布時間。 在列表中填充該時間。

如果您不清楚我的意思,請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM