2

I have a product list with expired date. Now I am trying to compare the dates from list view with current date. If product's expired date before current date then I want to do something. I am trying like below, but did not getting expecting result. Would someone help me please to do that?Thanks!

Code Snippet:

String pdate = product.get(ListViewActivity.KEY_DATE);

String pattern = "dd-MM-yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
Date Date1 = formatter.parse(currentDate);
Date Date2 = formatter.parse(pdate);
//if(Date1.compareTo(Date2)){}
if ((Date1.compareTo(Date2)) > 0){
     Toast.makeText(this.activity, "Expired", Toast.LENGTH_SHORT).show();
     //do something 
}

Full code in ListProductAdapter.java:

public class ListProductAdapter extends BaseAdapter {

private Activity activity;
private ArrayList<HashMap<String, String>> data;
private static LayoutInflater inflater = null;

public ListProductAdapter(Activity a, ArrayList<HashMap<String, String>> d){
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}


@Override
public int getCount() {
    return data.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

        ListProductViewHolder holder = null;

        if(convertView == null){
            holder = new ListProductViewHolder();
            convertView = LayoutInflater.from(activity).inflate(R.layout.list_row, parent, false);

            holder.productName = convertView.findViewById(R.id.title); // title
            holder.productDescription = convertView.findViewById(R.id.description); // description
            holder.productDate = convertView.findViewById(R.id.duration); // expiry time

            convertView.setTag(holder);
        }else{
            holder = (ListProductViewHolder) convertView.getTag();
        }
        holder.productName.setId(position);
        holder.productDescription.setId(position);
        holder.productDate.setId(position);

        HashMap<String, String> product = new HashMap<String, String>();
        product = data.get(position);

        try {
            holder.productName.setText(product.get(ListViewActivity.KEY_PRODUCT_NAME));
            holder.productDescription.setText(product.get(ListViewActivity.KEY_DESCRIPTION));
            holder.productDate.setText(product.get(ListViewActivity.KEY_DATE));

            String currentDate = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
            String pdate = product.get(ListViewActivity.KEY_DATE);

            String pattern = "dd-MM-yyyy";
            SimpleDateFormat formatter = new SimpleDateFormat(pattern);
            Date Date1 = formatter.parse(currentDate);
            Date Date2 = formatter.parse(pdate);
            if ((Date1.compareTo(Date2)) > 0){
               Toast.makeText(this.activity, "Expired", Toast.LENGTH_SHORT).show();
               //do something 
            }
        }catch (Exception e){

        }

        return convertView;
    }
}


class ListProductViewHolder {
    TextView productName;
    TextView productDescription;
    TextView productDate;

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
St Smith
  • 21
  • 4
  • Which is the minimum API level you are supporting? If you can rely on Java 8, do it with `java.time`, if you can use the **ThreeTenABP** mentioned in [this question](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project) then use that... – deHaar Dec 19 '19 at 08:44
  • I am using `minSdkVersion 15` and `openjdk version "11.0.5" 2019-10-15` – St Smith Dec 19 '19 at 08:52
  • Seriously, use `java.time` via [ThreeTenABP](https://github.com/JakeWharton/ThreeTenABP) and stop using `java.util.Date`... – deHaar Dec 19 '19 at 08:57

2 Answers2

0

Try this;

productDate.getTime() - System.currentTimeMillis() > 0
System.currentTimeMilis() - productDate.getTime() > 0

this will give you difference between two dates in long value. if you want, you can convert from long to date.

I dont know which one is true for you. You can check it easily and use it.

currentDate = System.currentTimeMillis(); productDate = which expiry date of your products

if your currentDate is different from current date, you can change it like this; currentDate.getTime()

JDevoloper
  • 227
  • 4
  • 17
0

Try this:

 Date currentTime = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
 Calendar c = Calendar.getInstance();
 c.setTime(currentTime);
xoned
  • 2,701
  • 2
  • 31
  • 43