Java Android:时间戳中不显示消息创建时间

Java Android:时间戳中不显示消息创建时间,java,android,datetime,timestamp,Java,Android,Datetime,Timestamp,我必须在时间戳中显示聊天应用程序的消息创建时间现在我所有的时间戳它会在我打开应用程序时自动显示设备时间它不会显示消息创建时间。检查我的代码,如果我做错了,请更改我 public static String getFormatedTime(long dateTime,String timeZone, String format){ String time = null; try{ Calendar cal = Calendar.getInstance();

我必须在时间戳中显示聊天应用程序的消息创建时间现在我所有的时间戳它会在我打开应用程序时自动显示设备时间它不会显示消息创建时间。检查我的代码,如果我做错了,请更改我

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();

但是我不知道如何通过使用前面的代码来实现它。

只需使用下面的代码就可以在创建消息时获得消息

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服务,都会及时收到消息。在列表中填充该时间

如果您不清楚,请告诉我。

c1.get(Calendar.MONTH)很难提供直观的月数(索引从零开始)。我认为,@Android_learner第一个想要月作为缩写,第二个在代码组织方面有相当大的问题,因为他自己已经完成了解决方案的所有部分。