Dopo vari tentativi falliti con le date in formato stringa, ho convertito le mie date stringa in oggetti Calendar e poi in tempo Unix prima di inserirle nel database SQLite, riconvertendole all'occorrenza (Unix → Calendar → stringa) in fase di visualizzazione.
Il tempo Unix permette di eseguire calcoli e confronti sulle colonne data — ordinamento, sorting crescente, BETWEEN e così via — e dopo molte ore di prove si è rivelato l'approccio migliore. Questo è il codice su cui mi sono attestato:
Cursor c = newDB.rawQuery("select ID, Date, Hours from " + tableName + " where Date BETWEEN '" + startDateQueryDate + "' AND '" + endDateQueryDate + "' ORDER BY Date ASC", null); if (c != null ) { if (c.moveToFirst()) { do { int tempId = c.getInt(c.getColumnIndex("ID")); long tempUnixTime = c.getLong(c.getColumnIndex("Date")); //convert tempUnixTime to Date java.util.Date startDateDate = new java.util.Date(tempUnixTime); //create SimpleDateFormat formatter SimpleDateFormat formatter1; formatter1 = new SimpleDateFormat("dd/MM/yyyy", Locale.UK); //convert Date to SimpleDateFormat and convert to String String tempStringStartDate = formatter1.format(startDateDate); int tempHours = c.getInt(c.getColumnIndex("Hours")); results.add(+ tempId + " Date: " + tempStringStartDate + " Hours: " + tempHours); }while (c.moveToNext()); } }
Suggerimento: usa i parametri selectionArgs al posto della concatenazione di stringhe nella query: eviti errori di sintassi e ottieni una protezione automatica dalla SQL injection.