دنبال کننده ها

۱۳۹۶ مهر ۷, جمعه

android - How to get latest 7 days and order it if date format is yyyyMdd?

[ad_1]



I try to describe my code more clearly.



So here is my issue:



I'm stuck with my database , i want to search latest 7 days , but i store my date format is yyyymdd like 2017/9/30
enter image description here
If i want to search all of my records and order it , my succeed code like this:



public List<Contact> sortingDate() 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;



It will print:
enter image description here



And then i try to add search latest days code to my function, i set a default date 2017/09/30



// order latest days
public List<Contact> latestDays() 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)) = 1n" +
" THEN '0'


I call my function:



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


It shows System.out: 0



So what code i miss it if i want to get latest 7 days like 2017/9/24 2017/9/29 2017/9/30 in my database ?



Any help would be appreciated. Thanks in advance.




[ad_2]

لینک منبع