如果日期格式为yyyyMdd,如何获取最新7天并订购?

时间:2017-09-30 05:22:29

标签: android android-sqlite

我试着更清楚地描述我的代码。

所以这是我的问题:

我坚持使用我的数据库,我想搜索最近7天,但我存储的日期格式是yyyymdd,如2017/9/30 enter image description here 如果我想搜索我的所有记录并订购它,我的成功代码如下:

public List<Contact> sortingDate() {
        List<Contact> contactList = new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS +" ORDER BY\n" +
                "    SUBSTR(Date, 1, 4) DESC,\n" +
                "    CASE WHEN INSTR(SUBSTR(Date, 6), '/') = 2\n" +
                "         THEN '0' || SUBSTR(Date, 6, 1)\n" +
                "         ELSE SUBSTR(Date, 6, 2) END DESC,\n" +
                "    CASE WHEN LENGTH(SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1)) = 1\n" +
                "         THEN '0' || SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1)\n" +
                "         ELSE SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1) END DESC; ",null);
        //looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setDate(cursor.getString(1));
                contact.setBeforeMorning(cursor.getString(2));
                contact.setAfterMorning(cursor.getString(3));
                contact.setBeforeNoon(cursor.getString(4));
                contact.setAfterNoon(cursor.getString(5));
                contact.setBeforeNight(cursor.getString(6));
                contact.setAfterNight(cursor.getString(7));
                System.out.println("The result is :" + cursor.getString(1));
                //Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        return contactList;
    }

它将打印: enter image description here

然后我尝试将搜索最新日期代码添加到我的函数中,我设置了默认日期2017/09/30

// order latest days
    public List<Contact> latestDays() {
        List<Contact> contactList = new ArrayList<>();
        // Database time format is: yyyymdd
        Calendar mCalendar = new GregorianCalendar();
        int mYear = mCalendar.get(Calendar.YEAR);
        int mMonth = mCalendar.get(Calendar.MONTH)+1;
        int mDay = mCalendar.get(Calendar.DAY_OF_MONTH);
        String today = String.valueOf(mYear).concat(String.valueOf(mMonth).concat(String.valueOf(mDay)));
        System.out.println(today+"today");
        SQLiteDatabase db = this.getWritableDatabase();
        // WHERE date...it is what i try the code
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS +" WHERE date > (SELECT DATETIME('2017/09/30'," + " ' -7" + " day'))" +
                " ORDER BY\n" +
                " SUBSTR(Date, 1, 4) DESC,\n" +
                " CASE WHEN INSTR(SUBSTR(Date, 6), '/') = 2\n" +
                " THEN '0' || SUBSTR(Date, 6, 1)\n" +
                " ELSE SUBSTR(Date, 6, 2) END DESC,\n" +
                " CASE WHEN LENGTH(SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1)) = 1\n" +
                " THEN '0' || SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1)\n" +
                " ELSE SUBSTR(SUBSTR(Date, 6), INSTR(SUBSTR(Date, 6), '/') + 1) END DESC; ",null);

        //looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Contact contact = new Contact();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setDate(cursor.getString(1));
                contact.setBeforeMorning(cursor.getString(2));
                contact.setAfterMorning(cursor.getString(3));
                contact.setBeforeNoon(cursor.getString(4));
                contact.setAfterNoon(cursor.getString(5));
                contact.setBeforeNight(cursor.getString(6));
                contact.setAfterNight(cursor.getString(7));
                System.out.println("The result is :" + cursor.getString(1));
                //Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        return contactList;
    }

我打电话给我的功能:

private DatabaseHandler db;
contactList = db.latestDays();
System.out.println(contactList.size());

显示System.out: 0

如果我想在我的数据库中获得2017/9/24 2017/9/29 2017/9/30这样的最新7天,那么我想念它的代码是什么?

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:2)

这是一次真正的学习体验,由于目前的变化是Android和Java中日期的使用方式,这个答案需要修改。问题是如何在sqlite DB中找到7天范围之间的所有日期。首先,最重要的概念是如何在数据库中格式化这种格式10-18-2017,因为字符串有很多问题,而这种格式10182017不是非常易读,除非你不是计算机!所以存储像这样的日期10152017然后显示它们这样的格式10-18-2017我的数据库设计将日期变量声明为TEXT使用INTEGER建议测试不确定它会更好地工作,除非你使所有整数长度相同10182017我们在这个日期1012017使用和整数搜索模式有问题这里是查询代码享受

public List<DBModel> getDataFromDB(){
    //String query = "SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + " > 0 " + " ORDER BY " + Col_ID + " DESC ";
    /* Notice the SPACES before AND after the words WHERE ORDER BY ASC or DESC most of all the condition " > 0 "*/
    /* =+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=*/
   Cursor cursor = null;
    List<DBModel> modelList = new ArrayList<>();
    if(use == true){
        //String query = "SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + " ='" + selectSTRING + "'";
        String query = "SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + " >= " + "'" + selectSTRING + "'" + " AND " + Col_PURCHASE_DATE + " <= " + "'" + secondSTRING + "'";
        //String query = "SELECT * FROM " + TABLE_INFO + " WHERE " + Col_PURCHASE_DATE + " >= " + "'"+ 1012017 +"'" + " AND " + Col_PURCHASE_DATE + " <= " + "'"+ 10312017 +"'";
        db = this.getWritableDatabase();
        cursor = db.rawQuery(query,null);
    }
    if(use == false){
        String query = "SELECT * FROM " + TABLE_INFO;
        db = this.getWritableDatabase();
        cursor = db.rawQuery(query,null);
    }

    if (cursor.moveToFirst()){

        do {
            DBModel model = new DBModel();
            model.setRowid(cursor.getInt(0));
            model.setStation_Name(cursor.getString(1));
            model.setDate_of_Purchase(cursor.getString(2));
            model.setGas_Cost(cursor.getString(3));
            modelList.add(model);

            int sz = modelList.size();
            int out =   model.setRowid(cursor.getInt(0));
            String out1 =  model.setStation_Name(cursor.getString(1));
            String out2 =  model.setDate_of_Purchase(cursor.getString(2));
            String out3 = model.setGas_Cost(cursor.getString(3));
            System.out.println("==============getDataFromDB ID "+out);
            System.out.println("==============getDataFromDB Station "+out1);
            System.out.println("==============getDataFromDB Date "+out2);
            System.out.println("==============getDataFromDB Cost "+out3);
            System.out.println("======= ======getDataFromDB SIZE "+sz);

        }while (cursor.moveToNext());
    }
    db.close();
    cursor.close();
    return modelList;
}

这是一种处理长度仅为7或6个字符的日期的方法

                    if(searchTo.length()==7){
                    StringBuilder str = new StringBuilder(searchTo);
                    System.out.println("string = " + str);


                    str.insert(2, '0');
                    etToDate.setText(str);
                }else if (searchFrom.length() == 6){
                    StringBuilder str = new StringBuilder(searchFrom);
                    str.insert(0,'0');
                    str.insert(2,'0');
                    etFromDate.setText(str);
                }else {
                    etToDate.setText(searchTo);
                }