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

标签 java android android-calendar

我有一份过期的产品 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/

相关文章:

java - 更快地读取大文本文件

java - 无法在我的 Springboot 应用程序中检索模板

Java打印票

android lollipop - 日历事件位置丢失

java - 如何用变量初始化 Java 对象?

java - 在多个 Activity 中重用布局的最简单方法

android - 如果我通过 intent 通过 Twitter 分享,我怎么可能有短 URL

java - 关闭在适配器内启动的 Activity 后如何刷新先前的 fragment ?

android - Android EVENT_ID 在日历中是否唯一?

安卓 : How to set monthly and weekly repeating calendar events(for selected days) using CalendarContract?