在比较两个日期后,我在消息上方显示日期。如果我显示每个项目的日期,它工作正常。但是当我试图展示Today
,Yesterday
和date
时,它始终只显示date
。这里的日期并没有错,但它在格式化日期出错了。
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss");
try {
Date current_itemDate = simpleDateFormat.parse(chatMessage.getDateTime());
DateFormat outputFormat = new SimpleDateFormat("hh:mm a");
Date date = simpleDateFormat.parse(chatMessage.getDateTime());
holder.timeTextView.setText(outputFormat.format(date));
long previousTs = 0;
if (position > 0) {
ChatMessage pm = chatMessages.get(position - 1);
previousTs = simpleDateFormat.parse(pm.getDateTime()).getTime();
}
setTimeTextVisibility(current_itemDate.getTime(), previousTs, holder.textViewTimePeriod,
holder.dividerTop);
} catch (ParseException e) {
e.printStackTrace();
}
我正在使用逻辑显示日期或日期TextView
,如下所述。如果我做错了,请告诉我。
private void setTimeTextVisibility(long now_tm, long msg_tm, TextView timeText, View dividerTop) {
Calendar now_calendar = Calendar.getInstance();
Calendar msg_calendar = Calendar.getInstance();
now_calendar.setTimeInMillis(now_tm);
msg_calendar.setTimeInMillis(msg_tm);
boolean sameDay = now_calendar.get(Calendar.YEAR) == msg_calendar.get(Calendar.YEAR) &&
now_calendar.get(Calendar.MONTH) == msg_calendar.get(Calendar.MONTH)
&& now_calendar.get(Calendar.DAY_OF_MONTH) == msg_calendar.get(Calendar.DAY_OF_MONTH);
if (msg_tm == 0) {
timeText.setVisibility(View.VISIBLE);
dividerTop.setVisibility(View.VISIBLE);
if (now_calendar.get(Calendar.DATE) == msg_calendar.get(Calendar.DATE)) {
timeText.setText("Today");
} else if (!sameDay) {
timeText.setText("Yesterday");
} else {
timeText.setText("" + new SimpleDateFormat("dd MMM, yyyy").format(new Date(now_tm)));
}
} else {
if (sameDay) {
timeText.setVisibility(View.GONE);
dividerTop.setVisibility(View.GONE);
timeText.setText("");
} else {
timeText.setVisibility(View.VISIBLE);
dividerTop.setVisibility(View.VISIBLE);
timeText.setText("Today");
}
}
}
答案 0 :(得分:2)
尝试将您的日期转换为Millis
,然后使用这些<,>,<=,>=,==,!=
运算符进行比较或查看此link
.after
用于比较日期
答案 1 :(得分:2)
在这里,我想向其他开发人员展示我们如何在whatsapp应用程序之类的消息显示前一天。
select