0

I got a recyclerView that will populate according to a JSON result using databinding, what i want to do is to check/compare the start date & expired date to see is the "Deals" is expired (if expired a textview with text "DEALS EXPIRED" will appear)

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

Here is some question from me:
* should i use DATE / String to store the object?
* where should i perform this action? under my fragment / adapter?
Appreciate if any source / example provided.

<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"
      />
Shawn.Y
  • 181
  • 3
  • 14

5 Answers5

0

should i use DATE / String to store the object?

Yes you have to save both values in Date or string.

where should i perform this action? under my fragment / adapter?

You have to perform this function in your adapter. while populate data to view.

compare date :

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
}

Hope it will help you!!

Hemant Parmar
  • 3,924
  • 7
  • 25
  • 49
0

You can use timestamp to do this kind of time checking thing. Send start and end timestamp in your json. And you can do this in your adapter.

EA Rashel
  • 167
  • 2
  • 9
0

Yes, I think you should use Date to represent start date and whatever other date you have, it makes more sense.

I don't think your Adapter/View is the best place do to this conversion. You should either have your parser do that for you OR you could do some post processing on your List of objects once they have been fetched from the network. Preferably even on a background thread.

Parsing those Strings to Dates in your Adapter is kind of wasteful and might make your RecyclerView a bit laggy. It will cause for repeated calculations of the same values and possibly a lengthy date parsing on the UI thread.

elmorabea
  • 3,243
  • 1
  • 14
  • 20
0

* where should i perform this action? under my fragment / adapter? perform this in onBindViewHolder of your adapter,

@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);

        }
    }

add this method in your adapter,

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;
    }
}
hasan_shaikh
  • 1,434
  • 1
  • 15
  • 38
0
  • Firstly if you want change textview text to "DEALS EXPIRED" you should perform action under your adapter.
  • If you don't want parse string again you can store date as Date object.

You can parse your date like this.

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.
}
  • it will had a text view "DEALS EXPIRED" creates in xml, and set the visibility "gone", it should change the visibility as "Visible" after checking – Shawn.Y May 14 '18 at 09:59