0

I'm setting up an SQLite database on android studio that stores scheduled items, each containing a date variable as a long (date in millis).

I'm trying to create two functions that compare two dates in millis. The first function checks wether they are one week apart, the other function checks if they are one month apart (by the day, and not the exact time) so that:

weekApart(1621548000000, 1622073600000) = true, etc.

I'm doing this so that when when the user selects a date on the calendar, the current timestamp is recorded, and I can search the table for items which fall into the correct day or month.

Does anyone know how this could be done? Thank you for any answers.

Edit: Great answers, thank you all :D

Pepe
  • 59
  • 6
  • 1
    Work out the length of a week in milliseconds, then you're just comparing integers. – Michael May 20 '21 at 13:06
  • 3
    How do you plan on accounting for daylight saving time? Two instants "exactly a week apart in civil time" might be 7 * 24 + 1, or 7 * 24 - 1 hours apart... – Jon Skeet May 20 '21 at 13:07
  • Ah thank you both for answering, I figure I can just divide the current timestamp by a day in millis and check if if the two numbers are in a certain range, so (timestamp1, timestamp2, 7). But thank you for mentioning this, I didn't even think about accounting for daylight saving time, I'll have to try figure this out. – Pepe May 20 '21 at 13:24
  • 1
    [Calculate days between two Dates in Java 8](https://stackoverflow.com/q/27005861/10819573) – Arvind Kumar Avinash May 20 '21 at 14:51

2 Answers2

2

Well, there are some things to consider here.

  • Do you want to take time zones into account? How many days there are in between two dates, depends on the time zone where you are in. For example,

    2021-01-31T23:00:00Z      until 2021-02-03T11:00:00Z      is 4 days inclusive
    2021-02-01T01:00:00+02:00 until 2021-02-03T13:00:00+02:00 is 3 days inclusive
    

    Both start times have the same timestamp. The same counts for the end times.

  • What is a month? Again, this depends on whether you want to take time zones into account. And you must know the exact date the timestamp represents in order to calculate the number of months in between, because the length of a month depends on, well, the month and even the year.

If you don't care about time zones, you could just use

long days = ChronoUnit.DAYS.between(Instant.ofEpochMillis(a), Instant.ofEpochMillis(b));

Otherwise, you end up with something like

ZoneId zoneId = ZoneId.of("Europe/Amsterdam");
var start = Instant.ofEpochMilli(a).atZone(zoneId);
var end = Instant.ofEpochMilli(a).atZone(zoneId);
long days = ChronoUnit.DAYS.between(start, end);
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • @MahmoudKhorrami For Android under level 26 you can use java.time and other Java 8+ features through [desugaring](https://developer.android.com/studio/write/java8-support#library-desugaring). See [this answer by Arpit Bhoi](/a/73023269/5772882). – Ole V.V. Dec 25 '22 at 19:44
  • Nice. But Field requires API level 26 – Mahmoud Khorrami Dec 25 '22 at 16:48
1

You may find the difference in days between the two dates.

First, you can convert your long numbers to a calendar:

Calendar date1 = Calendar.getInstance();
date1.setTimeInMillis(1621548000000L);

Calendar date2 = Calendar.getInstance();
date2.setTimeInMillis(1622073600000L);

Next, you can find the difference in days:

long days = ChronoUnit.DAYS.between(date1.toInstant(), date2.toInstant());

Finally, you can compare if the difference is greater than 7 for a week. You can also apply the same logic for months, but you need to define how many days you will consider in a month.

felipecrp
  • 999
  • 10
  • 16
  • 3
    The whole `Calendar` thing is unnecessary, as you could call `Instant.ofEpochMillis(yourNumber)`. You shouldn't be using `Calendar` in the first place, since they're [troublesome](https://stackoverflow.com/q/1969442/507738). – MC Emperor May 20 '21 at 13:33
  • 1
    Nice. It would reduce the answer to a single line. The main contribution was the use of the `ChronoUnit.DAYS.between` method. – felipecrp May 20 '21 at 14:08