Compare dates (stored as string) in android sqlite database?

后端 未结 5 1100
不知归路
不知归路 2020-12-02 19:03

I store dates as String in my database, in this format:

YYYY-MM-DD HH:MM:SS

which is one of supported formats (http://www.sqli

5条回答
  •  暖寄归人
    2020-12-02 19:47

    Date filed in the SQLite table will be TEXT and date data should be stored as "2012-12-31 00:12:00" format that comparison dose not give you trouble and if we consider that fields are date_from and date_to and we are storing current date at to_date then we should get the current date as bellow and

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String to_date = sdf.format(new Date());
    

    and the SQL query should be

        crs = db.rawQuery(
          "SELECT _id, date_from, date_to, done_or_not, list_name " +
          "FROM list_tbl " +
          "WHERE " +
          "date_from <= Datetime('"+to_date+"') AND date_to >= Datetime('"+to_date+"')", null);
    

提交回复
热议问题