-1

So I'm having some issues in trying to do this, I already tried with this code:

  thatDay.set(Calendar.DAY_OF_MONTH,25);
  thatDay.set(Calendar.MONTH,7); // 0-11 so 1 less
  thatDay.set(Calendar.YEAR, 1985);

  Calendar today = Calendar.getInstance();

  long diff = today.getTimeInMillis() - thatDay.getTimeInMillis(); //result in millis

And I kinda know why this is not what I want because, the thing that I need is taking the first date that I insert in database, and I want that to be like (CURRENTDATE - TXTDATE == DAYS THAT LEFT)

David Conrad
  • 15,432
  • 2
  • 42
  • 54
  • 1
    Also, just because you are using Android, that doesn't mean you can't use the new and improved, much better Java date/time APIs. There is the [ThreeTen Backport](https://www.threeten.org/threetenbp/) project that brings the JSR-310 features to Java 7 and Java 6, and can be used on Android. – David Conrad Feb 24 '22 at 15:54
  • `long millisElapsed = Duration.between( LocalDate.of( 1985 , Month.AUGUST , 25 ).atStartOfDay( ZoneOffset.UTC ).toInstant() , Instant.now() ).toMillis() ;` Or, replace `ZoneOffset.UTC` with a `ZoneId` representing the time zone by which you want to determine first moment of the day. Each day starts earlier in the east than in the west. – Basil Bourque Feb 24 '22 at 17:31

1 Answers1

0

Maybe try this:

public long daysBetween(Calendar startDate, Calendar endDate) {
    return TimeUnit.MILLISECONDS.toDays(Math.abs(endDate.getTimeInMillis() - startDate.getTimeInMillis()));
}
lew
  • 70
  • 10