存储然后将日期(以String格式)与当前日期作为SQLite查询中的条件进行比较

时间:2017-09-15 13:14:01

标签: android sqlite date android-sqlite

我正在创建一个应用程序,其中我在SQLite数据库中为我的客户添加截止日期,然后想要检索到期日到达的客户端。意味着我想使用条件进行查询,其中截止日期是<当前日期,然后在RecyclerView中获取数据。我还使用SimpleDateFormat和Calendar类将数据库中的日期保存为yyyy-MM-dd格式的字符串。但问题是我的RecyclerView中没有显示任何内容。

这是我用来从数据库中获取数据的函数,其中CLDUE是截止日期列:

 public static Cursor getPayDueData(String act)
        {   act = "Active";
            SQLiteDatabase db = sqh.getReadableDatabase();
            Calendar c = Calendar.getInstance();
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            String CurrentDate = df.format(c.getTime());
            String[] payData = {CLID,CLNM,CLDUE,CLFEE};
            return  db.query(TBL_CLIENTS,payData," CLDUE <= " + CurrentDate + " AND CLACT = ? ",new String[]{act},null,null,null);
        }

这是要获取数据的Activity:

public class Payments extends AppCompatActivity {

ArrayList<PayRecInfo> payRecInfoArrayList;
RecyclerView rc;
PayRecyclerAdapter payRecyclerAdapter;
MyDb myDb;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_payments);
    myDb = new MyDb(this);
    rc = (RecyclerView)findViewById(R.id.recyclerView2);
    rc.setLayoutManager(new LinearLayoutManager(this));
    payRecInfoArrayList = new ArrayList<>();
    payRecyclerAdapter = new PayRecyclerAdapter(this, payRecInfoArrayList);
    rc.setAdapter(payRecyclerAdapter);

    retrievePayDueCl();
}

public void retrievePayDueCl()
{
        payRecInfoArrayList.clear();
        Cursor cursor = MyDb.TblClients.getPayDueData("Active");
        if(cursor != null)
        {
            cursor.moveToPosition(-1);
            while(cursor.moveToNext())
            {
                try{
                   int id2 = cursor.getColumnIndex(CLID);
                   int nm2 = cursor.getColumnIndex(CLNM);
                   int du2 = cursor.getColumnIndex(CLDUE);
                   int fee2 = cursor.getColumnIndex(CLFEE);
                   String PayID = cursor.getString(id2);
                   String PayName = cursor.getString(nm2);
                   String PayDue = cursor.getString(du2);
                   String PayFee = cursor.getString(fee2);

                   PayRecInfo payRecInfo = new PayRecInfo(PayID, PayName, PayDue, PayFee);
                   payRecInfoArrayList.add(payRecInfo);
                   payRecyclerAdapter.notifyDataSetChanged();
                   rc.setAdapter(payRecyclerAdapter);
                 }catch (Exception e){e.printStackTrace();}
            }
        }
}

这是我的RecyclerView适配器类:

class PayRecyclerAdapter extends RecyclerView.Adapter<PayRecyclerAdapter.MyHolder> {

Context ctx;
ArrayList<PayRecInfo> payRecInfo;

PayRecyclerAdapter(Context ctx, ArrayList<PayRecInfo> payRecInfo){
    this.ctx = ctx;
    this.payRecInfo = payRecInfo;
}


@Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view  = LayoutInflater.from(ctx).inflate(R.layout.custom_row_payments,parent,false);
    return new MyHolder(view);
}

@Override
public void onBindViewHolder(MyHolder holder, int position) {
    try
    {
      if(payRecInfo != null) {
      holder.PayId.setText(payRecInfo.get(position).getId2());
      holder.PayName.setText(payRecInfo.get(position).getName2());
      holder.PayDue.setText(payRecInfo.get(position).getDue2());
      holder.PayFee.setText(payRecInfo.get(position).getFee2());
      }
    }
    catch(Exception e){e.printStackTrace();}
}

@Override
public int getItemCount() {
    if(payRecInfo != null)
    return payRecInfo.size();
    else return 0;
}

class MyHolder extends RecyclerView.ViewHolder {
    TextView PayId, PayName, PayDue, PayFee;
    MyHolder(View itemView) {
        super(itemView);
        PayId = (TextView)itemView.findViewById(R.id.PayId);
        PayName = (TextView)itemView.findViewById(R.id.PayNameTxt);
        PayDue = (TextView)itemView.findViewById(R.id.PayDueDateTxt);
        PayFee = (TextView)itemView.findViewById(R.id.PayFeeTxt);
    }
}
}

以下是我如何将日期插入db:         public static long insertData(RecClNames recClNames)      {try {                 SQLiteDatabase db = sqh.getWritableDatabase();

            ContentValues cv = new ContentValues();

            cv.put(CLNM,recClNames.getName());
            cv.put(CLMNO,recClNames.getPhone());
            cv.put(CLHT,recClNames.getHeight());
            cv.put(CLAGE,recClNames.getAge());
            cv.put(CLWT,recClNames.getWeight());
            cv.put(CLEML,recClNames.getEmail());
            cv.put(CLADD,recClNames.getAddress());
            cv.put(CLFEE,recClNames.getFee());
            cv.put(CLDATE,recClNames.getDate());
            cv.put(CLDUE,recClNames.getDue());
            cv.put(CLDUR,recClNames.getDuration());
            cv.put(CLACT,recClNames.getActive());
            cv.put(CLSPCS,recClNames.getSpCase());

            return db.insert(TBL_CLIENTS,CLID,cv);}
        catch (Exception e){e.printStackTrace();}
            return 0; }

recClNames是arraylist对象。注册日期是当前日期,截止日期是1个月之后的日期。但截止日期已经到期,它没有在RecyclerView中显示

我猜问题是SQLite查询的条件,但是,我无法解决这个问题 请帮忙。

1 个答案:

答案 0 :(得分:0)

以下是如何比较日期

的想法
try{
 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

 String str1 = "9/10/2015";
 Date date1 = formatter.parse(str1);

 String str2 = "10/10/2015";
 Date date2 = formatter.parse(str2);

 if (date1.compareTo(date2)<0)
  {
     System.out.println("date2 is Greater than my date1");                         
  }

}catch (ParseException e1){
    e1.printStackTrace();
}