android sqlite中String日期查询转换操作

    xiaoxiao2021-03-25  109

    如果表结构是Date类型比较好办直接操作就可以了这个不多说; 如果表结构是String类型则需要在查询时把要查找的字段转换为Date来和其他的日期比较后作为查询条件 Cursor mCursor = db.query(true, DATABASE_SLEEP_TABLE, new String[] { KEY_SLEEP_ROWID, KEY_SLEEP_START_DATE, KEY_SLEEP_END_DATE, KEY_SLEEP_DEEPSLEEP, KEY_SLEEP_LIGHTSLEEP, KEY_SLEEP_ONLINE }, " **strftime('%s', "+KEY_SLEEP_END_DATE+")** BETWEEN strftime('%s', '"+normalStartDate+"') AND strftime('%s', '"+normalEndDate+"') " + "and strftime('%s', "+KEY_SLEEP_START_DATE+") BETWEEN strftime('%s', '"+normalStartDate+"') AND strftime('%s', '"+normalEndDate+"')" +"or strftime('%s', "+KEY_SLEEP_START_DATE+")<= strftime('%s', '"+normalStartDate+"')" + " and strftime('%s', "+KEY_SLEEP_END_DATE+") BETWEEN strftime('%s', '"+normalStartDate+"') AND strftime('%s', '"+normalEndDate+"') " +" or strftime('%s', "+KEY_SLEEP_END_DATE+")>=strftime('%s', '"+normalEndDate+"')" + "and strftime('%s', "+KEY_SLEEP_START_DATE+") BETWEEN strftime('%s', '"+normalStartDate+"') AND strftime('%s', '"+normalEndDate+"')" , null, null, null, null, null);

    strftime(‘%s’, “+KEY_SLEEP_END_DATE+”)就是把String字段转变为日期Date来进行比较操作

    转载请注明原文地址: https://ju.6miu.com/read-5434.html

    最新回复(0)