Android SQlite:按日期范围从今天开始向后查询表格行?



我以"MM/dd/yyy"的形式从服务器获取日期,然后使用以下函数将其转换为毫秒:

public static long getSimpleDateToMillis(String simpleDate) throws ParseException {
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
Date date = formatter.parse(simpleDate);
return date.getTime();
}

然后我把结果作为 int 保存到数据库中。

现在我陷入了对我来说似乎是死胡同的地方。我无法通过搜索和我的库存知识找到一种方法,了解如何通过project_date列过滤我的游标加载器,该列在数据库中保存为整数。

我将能够以何种方式进行查询,以便: 从项目表中选择当前和向后(昨天等(project_date的所有行。

我试过这个,但似乎真的不是答案。

String [] projection = new String []{};
String selection = "datetime("+ ProjectsEntry.COLUMN_PROJECT_DATE + "/1000, 'unixepoch') =? ";
String [] selectionArgs = new String[]{" date('now')"};
return new CursorLoader(this,
JobsContract.JobsEntry.CONTENT_URI,
projection,
selection,
selectionArgs,
null);

我还没有找到任何其他可以指向我的参考资料,所以我希望有人可能也遇到过这个问题。

这就是我做一些非常相似的事情的方式,但使用 full timesteamp 即 long 而不是 int。

首先,我有这种方法来获取时间戳,以获取截至午夜的今天日期/时间(酒吧 1 毫秒(:-

/**
*
* @return  1 millsecond before midnight today
*/
private long getDateTimeOfAllofToday() {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH,1); // tomorrow
cal.set(Calendar.MILLISECOND,0);
cal.set(Calendar.SECOND,0);
cal.set(Calendar.MINUTE,0);
cal.set(Calendar.HOUR_OF_DAY,0);
cal.add(Calendar.MILLISECOND,-1);
return cal.getTimeInMillis();
}

然后我创建相应的where子句,例如:-

filter = DBRulesTableConstants.RULES_ACTON_COL +
" <= " +
Long.toString(getDateTimeOfAllofToday());

这是通过 rawQuery 使用的,所以不完全是你想要的,但很容易将" <= "更改为" <=?"然后使用String [] selectionArgs = new String[]{Long.toString(getDateTimeOfAllofToday())};或修改后的版本以获取整数。

最新更新