【发布时间】:2016-05-23 14:24:16
【问题描述】:
我正在尝试从我的数据库中检索过去一周的值。现在我正在运行一个查询来检索当前时间和当前时间之间的值 - 7 天。我希望在过去 1 周的条形图中显示数据。所以通过这种方式,我的算法有效。但是,如果我想在星期一查看数据,SQL 查询会返回整个上周的值(因为它是今天 - 7 天),因此上周的值也会出现在图表上。但是,我只想查看从星期日开始的值。就像,如果我在周三打开应用程序,它应该显示自周日以来的情节,而不是自上周三以来的情节。我正在寻找从星期日开始每周检索数据的 SQL 查询。或者java是否有一个我可以根据日期(例如每个星期六)触发的函数来清除我的数据库中的所有数据,因为这样我的代码就可以工作了。
这是我现在的代码:
public Cursor getStepsWeek(long time_week) throws ParseException{
String time_past = String.valueOf(new SimpleDateFormat("dd-MM-yyyy ", Locale.getDefault()).format(time_week));
int ex = Integer.parseInt(time_past.split("-")[0]) - 7;
String extract = String.valueOf(ex+"-") + String.valueOf(new SimpleDateFormat("MM-yyyy HH:mm", Locale.getDefault()).format(time_week));
Date date = new SimpleDateFormat("dd-MM-yyyy HH:mm", Locale.getDefault()).parse(extract);
Log.d("time past",date.getTime()+"");
SQLiteDatabase liteDatabase = this.getReadableDatabase();
Cursor cursor = liteDatabase.rawQuery("select * from steps_table where time_steps between " + String.valueOf(date.getTime()) + " and " + (time_week), null);
return cursor;
}
【问题讨论】:
-
我知道,但是我的数据库实现有点不同,因此很混乱