我必须在聊天应用程序的时间戳中显示消息创建时间,现在我的所有时间戳会自动显示设备时间。当我打开应用程序时,它不显示消息创建时间。请检查我的代码并更改我,如果我做错了。
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] 删除。
我来说两句