0

How can I calculate the minutes between two dates?

Start Date 20/4/2021 09:00

End Date 28/4/2021 09:00

The result should be 11520 Minute

I want the minutes only without calculate days, hours or seconds, How can I do like this one?

LEO
  • 53
  • 7
  • Does this answer your question? [Android difference between Two Dates](https://stackoverflow.com/questions/21285161/android-difference-between-two-dates) – kelvin Apr 20 '21 at 06:26
  • @kelvin Yes but I want it to calculate minutes only, Anyway I found the answer, Thanks. – LEO Apr 20 '21 at 06:45

2 Answers2

1

You can use ZonedDateTime.until method like this:

ZonedDateTime start = ZonedDateTime.of(2021, 4, 20, 9, 0, 0, 0, ZoneId.of("GMT"));
ZonedDateTime end = ZonedDateTime.of(2021, 4, 28, 9, 0, 0, 0, ZoneId.of("GMT"));
long mins = start.until(end, ChronoUnit.MINUTES);
System.out.println(mins);
hata
  • 10,652
  • 6
  • 37
  • 62
1

First convert date to milliseconds and then subtract and convert back to minutes.

for eg,


long startDateMillis = startDate.getTime();
long endDateMillis = endDate.getTime();

long diffMillis = startDateMillis - endDateMillis;

long minutes = (diffMillis/1000)/60;

digiwizkid
  • 739
  • 1
  • 16
  • 27