简体   繁体   中英

How to get the values between current date and current date of previous year in sqlite in Android

I tried to get the data from sqlite database between current date one year before. I used the following code. But it didn't provide the data. Consider the current date is 25-10-2013. There is data for 25-09-2013. But the code inside do, doesn't execute. I don't get what is wrong in the code. The same way, I used for 3 months before and 6 months before, they work fine.

DateFormat format = new SimpleDateFormat("dd-MM-yyyy");
    Calendar cal11 =Calendar.getInstance();
    cal11.add(Calendar.MONTH, -12);
    String  one_year_before = cur_format1111.format(cal11.getTime());
    Log.v("one_year_before", one_year_before);

Query to select data:

 Cursor cur21 = db.rawQuery("SELECT * FROM " + TIMER_TABLE
                        + " WHERE  cur_date BETWEEN ?  AND ?", new String[] {
                        one_year_before, cur_form_Date });
if (cur21 != null) {
                if (cur21.moveToFirst()) {
                    do {

                        has_valuesyear = true;
                        String reader_name_db_service = cur21.getString(cur21
                                .getColumnIndex("username"));
                        Log.v("reader_name_db_service21",
                                reader_name_db_service);
                        if (reader_name_db
                                .equalsIgnoreCase(reader_name_db_service)) {
                            String tot_reading_time = cur21.getString(cur21
                                    .getColumnIndex("tot_reading"));
                            Log.v("tot_reading_time21", tot_reading_time);
                            String cur_date = cur21.getString(cur21
                                    .getColumnIndex("cur_date"));
                            Log.v("cur_date21", cur_date);
                            one_year_set.add(reader_name_db_service + "~"
                                    + cur_date + "~" + tot_reading_time);

                        }

                    } while (cur21.moveToNext());
                }
            }
            cur21.close();
            db.close();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");

You would first have to store your dates as character in ISO format . Then your query will work.

Maybe give this a shot

Cursor cursor = db.query(TABLE_NAME, String[] {"column1", "column2"}, "cur_date BETWEEN " + year_before + " AND " + year_after +";",null,null,null,null,null);

The string array of columns, in your case, will contain all of your columns. The last null 's are for ordering and limits.

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