0

I am searching record from Firebase Database on the formated date rang converted in String type. I am calling filtering query on requestPlaceDate.

Query query = ordersDatabaseRef.limitToFirst(1000).orderByChild(ConstanceFnc.requestPlacedDate).startAt(startDate).endAt(endDate);
    query.addListenerForSingleValueEvent(orderListener);

enter image description here

Firebase return data including previous and next dates, not return specific date range data, what I am expecting from a query on startAt() and endAt()

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Shoaib Anwar
  • 819
  • 2
  • 12
  • 29

1 Answers1

1

I am searching record from Firebase database on the formated date rang converted in String type.

You aren't getting the desired results because your requestPlacedDate field holds a String and not a Timestamp. When you order String elements, the order is lexicographical. I answered a similar question, so please check my answer from the following post:

To accomplish the correct order for your results, you should change the type of your field to be Timestamp, as explained in my answer from the following post:

Once your field will hold the Timestamp value, your Query will work perfectly fine.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I have Also tried this query with requestTimeStemp String startTimeStemp = ConstanceFnc.dateTimeToTimeStemp(startDate, "11:59 PM"); String endTimeStemp = ConstanceFnc.dateTimeToTimeStemp(endDate, "11:59 PM"); Log.e("TAG","Start time stemp " + startTimeStemp); – Shoaib Anwar Jul 07 '21 at 04:44
  • Query query = ordersDatabaseRef.limitToFirst(1000).orderByChild("requestTimeStemp").startAt(startTimeStemp).endAt(endTimeStemp); query.addListenerForSingleValueEvent(orderListener); – Shoaib Anwar Jul 07 '21 at 04:45
  • When you are passing String values as "11:59 PM", you'll never get the correct results. If you pass `long` values, then you'll get the expected results. But only if the timestamps are also set correct in the database. Meaning it should **not** be something like "11:59 PM", it should a long, right? – Alex Mamo Jul 07 '21 at 06:45
  • I am converting it to Long then String like bellow... String startTimeStemp = ConstanceFnc.dateTimeToTimeStemp(startDate, "11:59 PM"); here startTimeStemp is the long value in casted in string – Shoaib Anwar Jul 07 '21 at 06:53
  • No, that's not how to do it. Your `requestTimeStemp` field should contain in the database a **long** value like **1625640973**. When you search the database, you should perform a Query that look like this: `.orderByChild("requestTimeStemp").startAt(1625640972).endAt(1625640974)`. See the first one end with 2, the seond with 4. Give it a try and tell me if it works. – Alex Mamo Jul 07 '21 at 06:58
  • i have also tried it... startAt() and endAt() not accept long it accept String, number – Shoaib Anwar Jul 07 '21 at 07:01
  • i have convert the requestTimeStemp into string and Querying.. it giving expected result – Shoaib Anwar Jul 07 '21 at 07:03
  • startAt() and endAt() also accept numbers, not only strings. – Alex Mamo Jul 07 '21 at 07:06
  • So when saying "it giving expected result", it means that you solved the issue? – Alex Mamo Jul 07 '21 at 07:06
  • Yes I have change some scenario... the issue is solved Thank You – Shoaib Anwar Jul 07 '21 at 08:10