0

I'm saving my data inside files (Google Firebase.Firestore) that are given the date as an ID when created. Files are put inside collection "Events". The structure looks like this:

Document 14-Jan-2019
ID: 1547504172
pr: "true"
po: "false"

Document 17-Jan-2019
ID: 1547763372
pr: "false"

...

Document 20-Jan-2019
ID: 1548025154
pr: "true"

Document 21-Jan-2019
ID: 1548107873
pr: "true"

IDs are created like this:

long ID = (long) Date().getTime()/1000

There is another collection called "Habits" that contains HabitInfo objects.

Document pr (object HabitInfo)
name: "pr"
startDay: 1547504172 //14-Jan-2019
points: 12 ...

Each object has a startDay parameter that specifies the beginning of the week being observed.

I would like to get all files whose dates of creation (in form of long numbers) are less than seven days after my startDay. The time of the day doesn't matter, only the date.

I tried doing something like this:

CollectionReference colRef = db.collection("events");
Query query = colRef.whereGreaterThanOrEqualTo("ID", hInfo.startDay)
                    .whereLessThan("ID", hInfo.startDay + 7*24*60*60);

The problem with this is that time difference between two of my IDs can be more than 7 days (converted to seconds) because they are recorded at different times of the day. For example, Monday 14th at 7 A.M. and Monday 21st at 3 P.M. are more than seven days away when looking at values in seconds.

How can I get a range considering only dates?

js- m
  • 143
  • 9
  • Don't describe code, but share the minimal complete standalone code that reproduces the problem please. "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve)" – Frank van Puffelen Jan 22 '19 at 04:01
  • You need to decide on a time zone for the operation. At the point in time that your `long startDay` specifies it is not the same date everywhere on this planet. – Ole V.V. Jan 22 '19 at 08:02
  • It’s a job for java.time, the modern Java date and time API. [Tutorial here](https://docs.oracle.com/javase/tutorial/datetime/). If you are not yet on API level 26 you get java,time through the backport, ThreeTenABP, see [How to use ThreeTenABP in Android Project](https://stackoverflow.com/questions/38922754/how-to-use-threetenabp-in-android-project). – Ole V.V. Jan 22 '19 at 08:05
  • 1
    Your ID is in seconds since the epoch. You need the files from the start of the day indicated by `startDay` and the following week (end day exclusive). Convert the time to a date using `Instant.ofEpochSecond(startDay).atZone(yourDesiredZoneId).toLocalDate()`. To add 7 days use `.plusDays(7)`. To convert back to seconds since the epoch for comparison with the files, use `yourDate.atStartOfDay(yourDesiredZoneId).toInstant().getEpochSecond()`. – Ole V.V. Jan 22 '19 at 09:13

0 Answers0