我想要实现的是从 SQLite 数据库中检索当前日期前 3 天和当前日期后 3 天范围内的记录,包括当前日期。
如何做到这一点,请提出一些例子来尝试。
首先,存储日期的列应该是长类型。此列将存储日期从纪元开始的毫秒数。
现在查询
Calendar calendar = Calendar.getInstance(); // This will give you the current time.
// Removing the timestamp from current time to point to todays date
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
calendar.add(Calendar.DATE, -3); // Will subtract 3 days from today.
Date beforeThreeDays = calendar.getTime();
calendar.add(Calendar.DATE, 6); // Will be your 3 days after today
Date afterThreeDays = calendar.getTime();
db.query("Table", null, "YOUR_DATE_COLUMN >= ? AND YOUR_DATE_COLUMN <= ?", new String[] { beforeThreeDays.getTime() + "", afterThreeDays.getTime() + "" }, null, null, null);
Select *
From TABLE_NAME
WHERE DATEDIFF(day, GETDATE(), COLUMN_TABLE) <= 3