I want to filter in a recyclerView With Dates. To do this I make a query on the db SQLite, the dump in a cursor. I have created a method to which two variables type date di=fechaInicio
and df=FechaFin
are passed. My problem is that I do not know how to compare the dates and especially format them to be able to filter. This in my method code, I get an error at the time of formatting the date
private void consultarListaDietas(Date di, Date df) {
SQLiteDatabase db = conn.getReadableDatabase();
Dieta dieta = null;
Date date = new Date();
Cursor cursor = db.rawQuery("SELECT * FROM " + Utilidades.TABLA_DIETAS, null);
if (di == null && df == null) {
while (cursor.moveToNext()) {
dieta = new Dieta();
dieta.setId_Dieta(cursor.getInt(0));
dieta.setNoches(cursor.getInt(1));
dieta.setKm(cursor.getDouble(2));
String fecha = cursor.getString(3);
DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = iso8601Format.parse(fecha);
} catch (ParseException e) {
Log.e(TAG, "Parsing ISO8601 datetime failed", e);
}
dieta.setFecha(date);
dieta.setId_Usuario_FK(cursor.getInt(4));
dieta.setId_Proyecto_FK(cursor.getInt(5));
}
} else {
String fechaFin = df.toString();
String fechaInicio = di.toString();
cursor = null;
String[] args = new String[]{fechaInicio, fechaFin};
cursor = db.rawQuery("SELECT * FROM " + Utilidades.TABLA_DIETAS + " WHERE Fecha BETWEEN ? AND ?", args);
while (cursor.moveToNext()) {
dieta = new Dieta();
dieta.setId_Dieta(cursor.getInt(0));
dieta.setNoches(cursor.getInt(1));
dieta.setKm(cursor.getDouble(2));
String fecha = cursor.getString(3);
DateFormat iso8601Format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
date = iso8601Format.parse(fecha);
} catch (ParseException e) {
Log.e(TAG, "Parsing ISO8601 datetime failed", e);
}
dieta.setFecha(date);
dieta.setId_Usuario_FK(cursor.getInt(4));
dieta.setId_Proyecto_FK(cursor.getInt(5));
}
}
}
I pass the date selection code:
@Override
public void onClick(View v) {
//método para mostrar fecha cuando pinche el botón
if (v==btinicio) {
dia = calendario.get(Calendar.DAY_OF_MONTH);
mes = calendario.get(Calendar.MONTH);
ano = calendario.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
txtinicio.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
}
}, ano,mes,dia);
datePickerDialog.show();
}
if (v == btfin) {
dia = calendario.get(Calendar.DAY_OF_MONTH);
mes = calendario.get(Calendar.MONTH);
ano = calendario.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
txtfin.setText(dayOfMonth + "/" + (month + 1) + "/" + year);
}
}, ano,mes,dia);
datePickerDialog.show();
}
}