這裏是我的數據庫結構日期如何獲得最後7天: 當日期格式yyyymdd
我想獲得3分的結果是2017/9/22
2017/9/24
2017/9/25
如果今天是2017/9/29
。
在我的數據庫處理器類我曾嘗試下面的代碼:
public List<Contact> latestDays() {
List<Contact> contactList = new ArrayList<>();
// My 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();
String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE date > (SELECT DATETIME('" + today + "'," + "'-7" + " day'))";
Cursor cursor = db.rawQuery(selectQuery, 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;
}
我把它放在我的MainActivity這樣的:
private DatabaseHandler db;
contactList = db.latestDays();
System.out.println(contactList.size());
我得到了我的所有記錄,但它不是我想要的。
原因可能是我的日期格式,所以我加我排序的所有日期的年月日至raqQuery:
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CONTACTS +" WHERE date > (SELECT DATETIME('" + today + "'," + "'-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);
我仍然得到同樣的結果...
所以我試圖找到另一種解決方案,並嘗試這樣,我更改SQL搜索語法:
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");
Calendar theEnd = Calendar.getInstance();
Calendar theStart = (Calendar) theEnd.clone();
theStart.add(Calendar.DAY_OF_MONTH, -7);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-M-dd");
String start = dateFormat.format(theStart.getTime());
String end = dateFormat.format(theEnd.getTime());
// Now you have date boundaries in TEXT format
Cursor cursor = db.rawQuery("SELECT * FROM contacts WHERE date BETWEEN "+start+" AND "+end +
" 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);
如果我的數據庫時間格式爲yyyymdd,我如何得到最新的7天?
你能共享項目/嘲笑在github上項目這個
變化? – deadfish
爲你明智的存儲日期在一些可排序的格式... fx作爲'LONG'(如milis)或格式爲'yyyy/mm/dd'(日期和飛蛾與前導零)......然後顯然**即使排序爲字符串** 2017/10/1'將大於'2017/9/31'(對於您的格式不適用),因爲9> 1 ......這都是因爲**沒有DATETIME類型在sqlite **(在2017-09) – Selvin
@deadfish你是不是要把這個文件上傳到github上? –