【问题标题】:Compare two date from JSON result in RecyclerView在 RecyclerView 中比较 JSON 结果中的两个日期
【发布时间】:2018-10-23 22:22:07
【问题描述】:

我有一个 recyclerView,它将使用 databinding 根据 JSON 结果填充,我想要做的是 检查/比较开始日期和过期时间日期查看“交易”是否已过期(如果过期,将出现带有文本“交易已过期”的文本视图)

"DealsPageInfo": [
    {
      "DisplayName": "string",
      "StartDate": "2018-04-27T03:06:18.890Z",
      "ExpiredDate": "2018-04-27T03:06:18.890Z",
      "Url": "string",
      "ImageUrl": "string",
      "ShortDescription": "string"
    }

这是我的一些问题:
* 我应该使用 DATE / String 来存储对象吗?
* 我应该在哪里执行此操作?在我的片段/适配器下?
如果提供任何来源/示例,请欣赏。

<TextView
      android:id="@+id/list_offers_startDate"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="3dp"
      android:text="@{offers.StartDate}"
      android:textColor="@color/colorGrayText"
      android:textSize="@dimen/list_fontsize"
      />

 <TextView
      android:id="@+id/list_offers_endDate"
      android:visibility="gone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="3dp"
      android:text="@{offers.ExpiredDate}"
      android:textColor="@color/colorGrayText"
      android:textSize="@dimen/form_fontsize"
      />

【问题讨论】:

标签: android datetime android-recyclerview android-databinding


【解决方案1】:

* 我应该在哪里执行此操作?在我的片段/适配器下? 在适配器的 onBindViewHolder 中执行此操作,

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position){
        if (isDateExpired(listItems.get(position).getStartDate(),listItems.get(position).getEndDate())){
            viewHolder.dealsExpiredtextview.setVisibility(View.GONE);
        }else {
            viewHolder.dealsExpiredtextview.setVisibility(View.VISIBLE);

        }
    }

在您的适配器中添加此方法,

public boolean isDateExpired(String startDate, String endDate) {
    SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy");
    boolean b = false;
    try {
        if (dfDate.parse(startDate).before(dfDate.parse(endDate))) {
            return true;  // If start date is before end date.
        } else if (dfDate.parse(startDate).equals(dfDate.parse(endDate))) {
            return false;  // If two dates are equal.
        } else {
            return false; // If start date is after the end date.
        }
    } catch (ParseException e) {
        e.printStackTrace();
        return false;
    }
}

【讨论】:

  • isDateExpired 没有任何返回项,如果我删除 try 并替换为 throw 异常,它似乎不起作用。
  • 日期格式会对此有影响吗?因为我的 JSON 结果格式是“yyyy-MM-ddThh-mm-ss”
  • isDateExpired 返回一个布尔值。仅使用 try catch
  • 我正在使用 isDateExpired 函数,但由于缺少返回语句而出错。
  • 如果我想与当前日期比较,我可以用 new Date() 替换开始日期吗?
【解决方案2】:

我应该使用 DATE / String 来存储对象吗?

是的,您必须将这两个值都保存在日期或字符串中。

我应该在哪里执行此操作?在我的片段/适配器下?

您必须在适配器中执行此功能。同时填充要查看的数据。

比较日期:

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
Date StartDate= sdf.parse(StartDate);
Date ExpiredDate= sdf.parse(ExpiredDate);
if (StartDate.after(ExpiredDate)) {
    //create your logic here
}

希望对你有帮助!!

【讨论】:

  • 谢谢!这会有所帮助,尽管我仍在研究如何在我的代码下使用它。
  • 你已经接受了其他人的回答,还在琢磨如何使用。奇怪!!
  • 接受的答案是有效的,但仍然需要进行细微的更改以适应我的代码
【解决方案3】:

你可以使用时间戳来做这种时间检查的事情。在您的 json 中发送开始和结束时间戳。您可以在适配器中执行此操作。

【讨论】:

    【解决方案4】:

    是的,我认为您应该使用Date 来表示开始日期以及您拥有的任何其他日期,这样更有意义。

    我不认为您的适配器/视图是进行此转换的最佳位置。您应该让解析器为您执行此操作,或者您可以在从网络获取对象后对您的 List 对象进行一些后期处理。最好甚至在后台线程上。

    Strings 中的Dates 解析为Adapter 有点浪费,并且可能会使您的RecyclerView 有点滞后。这将导致重复计算相同的值,并可能在 UI 线程上进行冗长的日期解析。

    【讨论】:

      【解决方案5】:
      • 首先,如果您想将 textview 文本更改为“DEALS EXPIRED”,您应该在您的适配器下执行操作。
      • 如果您不想再次解析字符串,可以将日期存储为 Date 对象。

      你可以像这样解析你的日期。

      Instant instant = Instant.parse( "2018-04-27T03:06:18.890Z" );
      Date date = java.util.Date.from( instant );
      if ( Calendar.getInstance().after( date ) ) {
          // if this statement is true deal is expired.
      }
      

      【讨论】:

      • 它将在xml中创建一个文本视图“DEALS EXPIRED”,并将可见性设置为“消失”,检查后应将可见性更改为“可见”
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-30
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多