gpt4 book ai didi

java - 将 ListView 日期与当前日期进行比较

转载 作者:行者123 更新时间:2023-11-29 02:17:37 25 4
gpt4 key购买 nike

我有一份过期的产品 list 。现在我正在尝试将 ListView 中的日期与当前日期进行比较。如果产品的过期日期早于当前日期,那么我想做点什么。我正在尝试如下,但没有得到预期的结果。有人可以帮我做吗?谢谢!

代码 fragment :

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

这将为您提供两个日期之间的长值差异。如果需要,您可以从长转换为日期。

我不知道哪一个适合你。您可以轻松检查并使用它。

currentDate = System.currentTimeMillis();productDate = 您产品的有效期

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

关于java - 将 ListView 日期与当前日期进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59405909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com