我有一个过期的产品清单。现在,我试图将列表视图中的日期与当前日期进行比较。如果产品的到期日期早于当前日期,那么我想做些事情。我正在尝试如下,但没有得到预期的结果。有人可以帮我吗?谢谢!
代码段:
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;
}
答案 0 :(得分:0)
尝试一下;
productDate.getTime() - System.currentTimeMillis() > 0
System.currentTimeMilis() - productDate.getTime() > 0
这将为您提供两个日期之间的长时差。如果需要,可以从长日期转换为日期。
我不知道哪一个适合您。您可以轻松检查并使用它。
currentDate = System.currentTimeMillis();
productDate =您的产品的失效日期
如果currentDate与当前日期不同,则可以像这样更改它; currentDate.getTime()
答案 1 :(得分:0)
尝试一下:
Date currentTime = new Date(System.currentTimeMillis() - 24 * 60 * 60 * 1000);
Calendar c = Calendar.getInstance();
c.setTime(currentTime);