Taha Sami
Taha Sami

Reputation: 1697

Calculate minutes between two dates

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?

Upvotes: 0

Views: 185

Answers (2)

digiwizkid
digiwizkid

Reputation: 811

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;

Upvotes: 1

hata
hata

Reputation: 12478

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);

Upvotes: 1

Related Questions