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;
}