【问题标题】:Displaying wrong date in RecyclerView Adapter在 RecyclerView 适配器中显示错误的日期
【发布时间】:2018-02-07 16:51:20
【问题描述】:

在比较两个日期后,我在消息上方显示日期。如果我显示每个项目的日期,它工作正常。但是当我试图显示TodayYesterdaydate 时,它总是只显示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");
        }

    }
}

【问题讨论】:

    标签: android datetime android-recyclerview


    【解决方案1】:

    在这里,我想向其他开发人员展示我们如何像 whatsapp 应用程序那样显示消息的前一天。

     private void setTimeTextVisibility(long now_tm, long msg_tm, TextView timeText, View dividerTop) {
        Date nowDate = new Date();
        nowDate.setTime(now_tm);
        Date msgDate = new Date();
        msgDate.setTime(msg_tm);
        Calendar now_calendar = Calendar.getInstance();
        now_calendar.setTimeInMillis(now_tm);
        Calendar now = Calendar.getInstance();
        Calendar msg_calendar = Calendar.getInstance();
        msg_calendar.setTimeInMillis(msg_tm);
        if (msg_tm == 0) {
            timeText.setVisibility(View.VISIBLE);
            dividerTop.setVisibility(View.VISIBLE);
            if (DateUtils.isToday(now_calendar.getTimeInMillis())) {
                timeText.setText("Today");
            } else if (now.get(Calendar.DATE) - now_calendar.get(Calendar.DATE) == 1) {
                timeText.setText("Yesterday");
            } else
                timeText.setText("" + new SimpleDateFormat("dd MMM, yyyy").format(new Date(now_tm)));
        } else {
            if (msgDate.before(nowDate)) {
    
                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);
                Log.e("YESTERDAY", (now.get(Calendar.DATE) - msg_calendar.get(Calendar.DATE) == 1) + "");
                if (sameDay) {
                    timeText.setVisibility(View.GONE);
                    dividerTop.setVisibility(View.GONE);
                    timeText.setText("");
                } else {
                    timeText.setVisibility(View.VISIBLE);
                    dividerTop.setVisibility(View.VISIBLE);
                    if (DateUtils.isToday(now_calendar.getTimeInMillis())) {
                        timeText.setText("Today");
                    } else if (now.get(Calendar.DATE) - now_calendar.get(Calendar.DATE) == 1) {
                        timeText.setText("Yesterday");
                    } else {
                        timeText.setText("" + new SimpleDateFormat("dd MMM, yyyy").format(new Date(now_tm)));
                    }
                }
            } else {
                timeText.setVisibility(View.GONE);
                dividerTop.setVisibility(View.GONE);
                timeText.setText("");
            }
        }
    
    
    }
    

    【讨论】:

      【解决方案2】:

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

      .after 用于比较日期

      【讨论】:

      • 是的,这是一个很好的方法我还使用.before函数改进了我的代码sn-p。
      猜你喜欢
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 1970-01-01
      • 2020-07-02
      • 2021-11-29
      相关资源
      最近更新 更多