java - 将列表视图日期与当前日期进行比较
问题描述
我有一个过期日期的产品列表。现在我正在尝试将列表视图中的日期与当前日期进行比较。如果产品的过期日期早于当前日期,那么我想做点什么。我正在尝试如下,但没有得到预期的结果。有人可以帮我这样做吗?谢谢!
代码片段:
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
这将为您提供两个长值日期之间的差异。如果需要,您可以从 long 转换为 date。
我不知道哪一个对你来说是正确的。您可以轻松检查并使用它。
currentDate = System.currentTimeMillis();
productDate = 您的产品的有效期
如果您的 currentDate 与当前日期不同,您可以像这样更改它;currentDate.getTime()
推荐阅读
- django - 如何在维护外键和多对多关系的同时将 Django PostgreSQL 数据库模型的主键还原为 AutoField
- xml - 如何为 xsd:element 定义命名空间以覆盖默认的 targetNamespace
- list - 如何使用 Hibernate 的 PersistentBag 不遵守列表等于合同?
- javascript - WSL 上的节点 10 与节点 8:getTimezoneOffset 的值不同
- swift - 在 Swift 中编写 Kotlin 的副句(又名类委托)的正确方法是什么?
- python - 此代码中的第 5 行有什么问题?其他线路有什么问题吗?
- sql - 如何在Sql中检索单行
- mysql - 如何使用 json 对象通过逗号分解字符串
- ruby-on-rails - 如何在Rails 5中使用Capybara单击具有相同文本的链接之一
- javascript - Reactjs PWA 闪屏在 iOS 上不起作用