简体   繁体   中英

SQL query based on date and day

I am trying to retrieve values from my database for the past one week. Right now I am running a query to retrieve values between current time and current time - 7days. I wish to display data in a barchart for the past 1 week. So in this way, my algorithm works. But if I want to view data on a Monday, the SQL query returns values from the entire last week (since it does Today - 7 days) and so values from last week also pop up on graph. However I just want to view values from Sunday onwards. Like, if I open the app on wednesday, it should show me plot since the Sunday and not since the previous wednesday. I am looking for an SQL query which retrieves data on a weekly basis starting sunday. Or does java have a function which I can fire based on a date (every saturday for instance) to clear all data from my db, coz that way my code will work.

here's my code right now:

  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;
}

Make up your mind. Do you want data "for the past one week", or do you want data for this week only (since Sunday).

Anyway, here is general Java code for identifying the Date of the most recent Sunday, returning today if today is Sunday.

// Get "now"
Calendar cal = Calendar.getInstance();

// Update to most recent "first day of week",  e.g. SUNDAY in the U.S., MONDAY in France
int firstDayOfWeek = cal.getFirstDayOfWeek();
int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
if (dayOfWeek < firstDayOfWeek)
    dayOfWeek += 7;
cal.add(Calendar.DAY_OF_MONTH, firstDayOfWeek - dayOfWeek);

// Remove time fields
int year  = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day   = cal.get(Calendar.DAY_OF_MONTH);
cal.clear();
cal.set(year, month, day);

// Get a Timestamp for use with PreparedStatement
Timestamp startDate = new Timestamp(cal.getTimeInMillis());

To get first day of year, use:

Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
cal.clear();
cal.set(year, Calendar.JANUARY, 1);
Timestamp startDate = new Timestamp(cal.getTimeInMillis());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM