【发布时间】:2017-04-03 08:28:55
【问题描述】:
我有 ListView,它像任何聊天应用程序一样显示消息和时间。我想在 ListView 中显示像 Today、Yesterday 这样的时间戳以及日期。我已将它设置到适配器中,但它显示在所有行中,但我只想像昨天一样显示开始,所有消息都在它下面,今天消息应该显示今天的消息。 卡住了,求各位大神帮忙解决!!!
我已经尝试过了,它工作正常,但我只想显示在一个行项目中而不是所有行项目中
public String getSmsTodayYestFromMilli(long msgTimeMillis) {
Calendar messageTime = Calendar.getInstance();
messageTime.setTimeInMillis(msgTimeMillis);
// get Currunt time
Calendar now = Calendar.getInstance();
final String strTimeFormate = "h:mm aa";
final String strDateFormate = "dd/MM/yyyy h:mm aa";
if (now.get(Calendar.DATE) == messageTime.get(Calendar.DATE)
&&
((now.get(Calendar.MONTH) == messageTime.get(Calendar.MONTH)))
&&
((now.get(Calendar.YEAR) == messageTime.get(Calendar.YEAR)))
) {
// return "today at " + DateFormat.format(strTimeFormate, messageTime);
return "Today";
} else if (
((now.get(Calendar.DATE) - messageTime.get(Calendar.DATE)) == 1)
&&
((now.get(Calendar.MONTH) == messageTime.get(Calendar.MONTH)))
&&
((now.get(Calendar.YEAR) == messageTime.get(Calendar.YEAR)))
) {
// return "yesterday at " + DateFormat.format(strTimeFormate, messageTime);
return "Yesterday";
} else {
mDay = DateFormat.format(strDateFormate, messageTime) + "";
// return "date : " + DateFormat.format(strDateFormate, messageTime);
return DateFormat.format(strDateFormate, messageTime) + "";
}
}
【问题讨论】:
标签: android listview timestamp message days