I have a typeconverter that goes like this
public class DateConverter {
@TypeConverter
public static Date convertLongToDate(Long dateLong) {
return dateLong == null ? null : new Date(dateLong);
}
@TypeConverter
public static Long convertDateToLong(Date date) {
return date == null ? null : date.getTime();
}
...
}
And I use it in my database
@Database(entities = { LoginEntity.class,
Transaction.class,
UnsentTagNumbers.class }, version = 11, exportSchema = false)
@TypeConverters(DateConverter.class)
public abstract class ApplicationDatabase extends RoomDatabase {
public abstract LoginDao loginDao();
...
}
In inserting the transaction to the database, I do it like so.
public void recordTransaction() {
transaction.setTransactionDate(Calendar.getInstance().getTime()); <==== Please take note on the way that I use the current date.
transaction.setTransactionType(getApplication().getString(R.string.exit_scan));
...
transactionRepository.insert(transaction);
}
In my DAO
@Query("SELECT * FROM TRANSACTION_TABLE WHERE transactionDate >= :fromDate AND transactionDate <= :toDate AND staffId = :staffId")
LiveData<List<Transaction>> getTransactionSummaryByDates(Date fromDate, Date toDate, String staffId);
This is how I search for the transactions by dates
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnStartDate:
if(!hasSelectedStaffId) {
Toast.makeText(this, "Select a staff id first.", Toast.LENGTH_SHORT).show();
return;
}
Calendar cldr = Calendar.getInstance();
int day = cldr.get(Calendar.DAY_OF_MONTH);
int month = cldr.get(Calendar.MONTH);
int year = cldr.get(Calendar.YEAR);
dpdStartDate = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
hasSelectedStartDate = true;
startDate = new Date(year, monthOfYear, dayOfMonth, 0, 0, 0);
btnStartDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
}
}, year, month, day);
dpdStartDate.show();
break;
case R.id.btnEndDate:
cldr = Calendar.getInstance();
day = cldr.get(Calendar.DAY_OF_MONTH);
month = cldr.get(Calendar.MONTH);
year = cldr.get(Calendar.YEAR);
dpdEndDate = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
endDate = new Date(year, monthOfYear, dayOfMonth, 23, 59, 59);
btnEndDate.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
if (hasSelectedStartDate && hasSelectedStaffId) {
viewModel.getTransactions(startDate, endDate, selectedStaffId);
transactionAdapter.setTransactionList(transactionList);
transactionAdapter.notifyDataSetChanged();
}
}
}, year, month, day);
dpdEndDate.show();
break;
}
}
And this is in my viewmodel to get the transactions
public void getTransactions(Date startDate, Date endDate, String staffId) {
transactionList = transactionRepository.getTransactionSummaryByDates(startDate, endDate, staffId);
}
How come, I do not get any information from the database? If I check my database, the transactions are recorded. Please see image.
This is the parameters I send so I can get via dates
startDate: Wed Jan 25 00:00:00 GMT+08:00 3922 endDate: Thu Jan 26 23:59:59 GMT+08:00 3922 staffId: 10164
But if I convert my dates that I get from the datepicker to epoch like this:
if (hasSelectedStartDate && hasSelectedStaffId) {
Long lStartDate = startDate.getTime();
Long lEndDate = endDate.getTime();
viewModel.getTransactions(lStartDate, lEndDate, selectedStaffId);
//viewModel.getTransactions(startDate, endDate, selectedStaffId);
//setupTransactionList();
}
I've noticed that in my database, the date is 16431... But when I query and I convert my date parameters, it is 6060....
How do I make this right?