如果今天是2017/9/22
,我想得到3个2017/9/24
2017/9/25
2017/9/29
的结果。
在我的DatabaseHandler类中,我尝试了以下代码:
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());
我得到了所有记录,但这不是我想要的。
原因可能是我的日期格式,所以我将yyyymmdd的所有日期排序添加到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天的时间吗?
答案 0 :(得分:1)
试试这个
final LocalDate currentDate= LocalDate.now();
final LocalDate date7Days = date.minusDays(7);
//Format and display date
final String formattedDate = date7Days.format(DateTimeFormatter.ISO_LOCAL_DATE);
System.out.println(formattedDate);
根据您的日期时间格式进行更改