我必须在聊天应用程序的时间戳中显示消息创建时间,现在我的所有时间戳都会自动显示设备时间当我打开应用程序时,它不会显示消息创建时间。检查我的代码并更改我,如果我做错了.
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;
}
我没有设置任何具体格式,但您可以根据您的要求进行调整。
希望它对你有用。
关于java - Android:时间戳中不显示消息创建时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20736336/