【问题标题】:How to set time like today, yesterday and date on particular item in ListView如何在 ListView 中的特定项目上设置今天、昨天和日期的时间
【发布时间】:2017-04-03 08:28:55
【问题描述】:

我有 ListView,它像任何聊天应用程序一样显示消息和时间。我想在 ListView 中显示像 TodayYesterday 这样的时间戳以及日期。我已将它设置到适配器中,但它显示在所有行中,但我只想像昨天一样显示开始,所有消息都在它下面,今天消息应该显示今天的消息。 卡住了,求各位大神帮忙解决!!!

我已经尝试过了,它工作正常,但我只想显示在一个行项目中而不是所有行项目中

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


    【解决方案1】:

    这是每个聊天应用程序的普遍问题。也许你可以从这个库中阅读源代码开始:Android Ago library

    【讨论】:

    【解决方案2】:

    看看getRelativeTimeSpanString这个方法来自DateUtils

    【讨论】:

      【解决方案3】:

      您已经比较了两个日期,第一个是 getView 中的位置,另一个是前一个项目(位置-1),请试试这个:

             try {
                  SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
                  Date date_ = format.parse("Item date time");
                  java.text.DateFormat df = java.text.DateFormat.getTimeInstance();
                  vh.chat_time_label_txt.setText(df.format(date_));
      
                  SimpleDateFormat dateFormat_ = new SimpleDateFormat(
                      "dd MMMM yyyy", Locale.ENGLISH);
                  vh.top_date_label_txt.setText(dateFormat_.format(date_));
                  vh.top_date_label_txt.setVisibility(View.VISIBLE);
                  if (position > 0) {
                      XmppChatModel temp = mChatConversationArrayList.get(mChatConversationArrayList.size() - 1);
                      Date date__ = format.parse(temp.getTIMESTAMP());
                      if (dateFormat_.format(date__)
                          .equals(dateFormat_.format(date_))) {
                          vh.top_date_label_txt.setVisibility(View.GONE);
                      }
                  }
            } catch (Exception e) {
                 e.printStackTrace();
                 vh.top_date_label_txt.setVisibility(View.GONE);
            }
      

      【讨论】:

        猜你喜欢
        • 2019-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-27
        • 1970-01-01
        相关资源
        最近更新 更多