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

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

我尚未设置任何特定格式,但是您可以根据需要进行调整。

希望对你有效。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android:消息创建时间未显示在时间戳中

来自分类Dev

在 dataGridView 中显示时间戳

来自分类Dev

如何创建仅在表中显示日期部分的时间戳

来自分类Dev

如何创建仅在表中显示日期部分的时间戳

来自分类Dev

SNS中的时间戳-> SQS消息正文

来自分类Dev

在Oracle中显示与时间戳关联的时区

来自分类Dev

在audibook中为字幕创建时间戳

来自分类Dev

在Firebird中创建整数时间戳

来自分类Dev

Grok重新创建时间戳和消息

来自分类Dev

以消息格式打印时间戳

来自分类Dev

在Android中从时间戳获取日期名称

来自分类Dev

Android Firebase 时间戳

来自分类Dev

在长时间的操作中显示消息吗?

来自分类Dev

仅在journalctl中打印时间戳和消息

来自分类Dev

如何最好地按时间戳保存聊天中的消息?

来自分类Dev

蜂巢中的时间戳?

来自分类Dev

时间戳未在mysql中转换

来自分类Dev

时间戳未在mysql中转换

来自分类Dev

如何显示从时间戳经过的时间?

来自分类Dev

HighChart时间戳现在突出显示时间

来自分类Dev

根据时间创建时间戳:PHP

来自分类Dev

在pyspark中的两个时间戳之间创建一个时间戳数组

来自分类Dev

反应-显示Firestore时间戳

来自分类Dev

在吐司上显示时间戳

来自分类Dev

从时间戳SQL创建队列

来自分类Dev

从猪创建蜂巢时间戳

来自分类Dev

Javascript从格式创建时间戳

来自分类Dev

KDB 创建微秒时间戳

来自分类Dev

如何在cshtml中按用户位置显示时间戳