1

I want to check two dates. If two dates are the same then recyclerview will show that day's sell report.

I set a date picker.

DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            calendar.set(Calendar.YEAR, year);
            calendar.set(Calendar.MONTH, month);
            calendar.set(Calendar.DAY_OF_MONTH, day);
            //mills = calendar.getTimeInMillis();
            dateChange();        
        }
    };

    tvDate.setOnClickListener(view -> {
        DatePickerDialog datePickerDialog = new DatePickerDialog(context, date, calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
        datePickerDialog.show();

    });

I want when I pick a date, recyclerview show report from SQLite database on that date. How I will check database date and my date picker date are the same?

I have tried this.

private void dateListArray() {  

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy");
    String currDate = simpleDateFormat.format(calendar.getTimeInMillis());

    for (ProductModel p : myDatabase.getAllReport()) {

        String dates = simpleDateFormat.format(new Date(p.getDate()));

        //check datepicker date and allreport date from database
        if (currDate == dates){           
            dateList.add(p)

        }else{

        }
    }
    adapter.setFilter(dateList);//filter recyclerview
}

please help.

sadik
  • 50
  • 1
  • 8
  • 1
    Consider not using `Date`, `Calendar` and `SimpleDateFormat`. Those classes are poorly designed and long outdated. Use `LocalDate` and `DateTimeFormatter` from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/index.html). For Android below API level 26 largely available through desugaring. – Ole V.V. Nov 10 '21 at 03:56
  • Under the linked original question the good answer is [the one by Istvan here](https://stackoverflow.com/a/30026560/5772882). Much more depth in [this answer by Basil Bourque](https://stackoverflow.com/a/43012329/5772882) under the other original. Still better if you can avoid using `Date` completely and just use `LocalDate.isEqual()`. See [answer by Lorcan O’Neill](https://stackoverflow.com/a/35411693/5772882). – Ole V.V. Nov 10 '21 at 04:12
  • Thacks O,Neill. I get my answer. I have use Local date. Its very easy and clear. But its need 26 API. – sadik Nov 16 '21 at 05:03
  • I read that for Android under API level 26 you can use `LocalDate` and other java.time classes though [Java 8+ APIs available through desugaring | Android Developers](https://developer.android.com/studio/write/java8-support-table). I am no Android developer myself. – Ole V.V. Nov 16 '21 at 05:30

1 Answers1

0
 if (currDate == dates){           
      dateList.add(p)
 }

java.utils.Date provided method getDate (deprecated since Java 8)

if you want to compare 2 Date, converted your date string value to java.utils.Date, use getDate then compare 2 values like so

if (currDate.getDate() == dates.getDate()){           
   dateList.add(p);
}
  • If get date deprecated then whats the easy way to check date. – sadik Nov 10 '21 at 03:53
  • 1
    Incorrect. I set the two dates to `new Date(1518297681000L)` and `new Date(1636516803331L)` and the condition turned out true in my time zone. The dates are Sat Feb 10 22:21:21 CET 2018 and Wed Nov 10 05:00:03 CET 2021, so not the same date at all. Also `getDate()` and most other methods of `Date` are deprecated since Java 1.1, February 1997, it will soon be 25 years. And deprecated for a reason: they work unreliably across time zones. – Ole V.V. Nov 10 '21 at 04:03
  • I think offsetDateTime is more efficient, I miss read the question, I was focusing in "Java Date" – Le Programmeur Nov 10 '21 at 04:21
  • 1
    I have found the solution. Locadate is easy to use and gives perfect solution. `localDate = instant.ofEpochMilli(calendar.getTimeInMillis()).atZone(ZoneId.systemDefault()).toLocalDate(); for (ProductModel p : myDatabase.getAllReport()) { databaseLocaldate = Instant.ofEpochMilli(p.getDate()).atZone(ZoneId.systemDefault()).toLocalDate(); if (localDate.equals(databaseLocaldate)) { dateList.add(p); } }` – sadik Nov 16 '21 at 05:08