将列表视图日期与当前日期进行比较



我有一个过期日期的产品列表。现在我正在尝试将列表视图中的日期与当前日期进行比较。如果产品的过期日期在当前日期之前,那么我想做点什么。我正在尝试如下,但没有得到预期的结果。有人会帮我这样做吗?谢谢!

代码片段:

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 
}

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

试试这个;

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

这将使您在多头值的两个日期之间产生差异。 如果需要,可以从"长"转换为"日期"。

我不知道哪一个对你来说是真的。您可以轻松检查并使用它。

当前日期 =System.currentTimeMillis();产品日期 = 产品的有效期

如果您的当前日期与当前日期不同,您可以像这样更改它;currentDate.getTime()

试试这个:

Date currentTime = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Calendar c = Calendar.getInstance();
c.setTime(currentTime);

最新更新