选择日期如何在ArrayList中查找?

时间:2016-06-01 05:35:18

标签: android for-loop

当我的日历中没有时间显示吐司时选择的日期"日期不在日历中"和吐司只会显示一次。

String selectedGridDate = CalendarAdapter.dayString.get(position);  //Selected Date today
if (!selectedDate.equals(""))
{
    ArrayList<Bookings> bookingsArrayList = new ArrayList<Bookings>();

    for(int i=0; i<bookingsArrayList.size(); i++)
    {
        if (bookingsArrayList.get(i).getFor_date().contains(selectedGridDate))
        {
            Toast.makeText(CalendarActivity.this, "Selected Date Have in Calendar", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(CalendarActivity.this, "Selected Date not in Caledar", Toast.LENGTH_SHORT).show();
        }
    }
}

这是我的模态类,我在for_date中使用for_date所有数据都来自数据库......

public class Bookings {
    String created_at;
    String for_date;
    String id;
    String property_id;
    String status;
    String updated_at;
    String customer_name;
    String customer_contact;

    public String getCustomer_name() {
        return customer_name;
    }

    public void setCustomer_name(String customer_name) {
        this.customer_name = customer_name;
    }

    public String getCustomer_contact() {
        return customer_contact;
    }

    public void setCustomer_contact(String customer_contact) {
        this.customer_contact = customer_contact;
    }

    public String getCreated_at() {
        return created_at;
    }

    public void setCreated_at(String created_at) {
        this.created_at = created_at;
    }

    public String getFor_date() {
        return for_date;
    }

    public void setFor_date(String for_date) {
        this.for_date = for_date;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getProperty_id() {
        return property_id;
    }

    public void setProperty_id(String property_id) {
        this.property_id = property_id;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getUpdated_at() {
        return updated_at;
    }

    public void setUpdated_at(String updated_at) {
        this.updated_at = updated_at;
    }
}

2 个答案:

答案 0 :(得分:0)

使用以下方法将日期字符串转换为实际日期:

 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd h:m");//this can be your custom format.

Date start = sdf.parse(selectedGridDate);
Date end = sdf.parse(bookingsArrayList.get(i).getFor_date());

if (start.compareTo(end) == 0) {
//your dates match
}

答案 1 :(得分:0)

int count = 0;
            if (!selectedDate.equals(""))
            {
                for(int i=0; i<bookingsArrayList.size(); i++)
                {
                    String getDate = bookingsArrayList.get(i).getFor_date();
                    if(getDate.contains(selectedGridDate))
                    {
                        count++;
                    }
                }
                if(count==1)

                {
                    Toast.makeText(CalendarActivity.this, "Selected Date here", Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(CalendarActivity.this, "not Selected Date here", Toast.LENGTH_SHORT).show();
            }