在RecyclerView适配器中显示错误的日期 [英] Displaying wrong date in RecyclerView Adapter

查看:94
本文介绍了在RecyclerView适配器中显示错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我在比较两个日期之后在消息上方显示日期.如果我在每个项目上都显示日期,则效果很好.但是当我尝试显示TodayYesterdaydate时,它始终仅显示date.这里的日期没有错,但是在格式化日期上却有些问题.

Here I'm showing date above messages after comparison two dates. If I show dates with each item it works fine. but when I'm trying to show Today, Yesterday and date its always showing date only. Here date is not wrong, but its something getting wrong on formatting 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.如果我做错了事,请告诉我.

I'm using the logic for showing TextView with dates or days is mentioned below. Please let me know if I'm doing something wrong.

 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");
        }

    }
}

推荐答案

尝试将日期转换为Millis,然后使用这些<,>,<=,>=,==,!=运算符进行比较,或选中此

Try to convert your date into Millis and then compare using these <,>,<=,>=,==,!= operators or check this link

.after用于比较日期

这篇关于在RecyclerView适配器中显示错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆