I've seen some examples using Joda Time and other methods to work out the difference between two dates in milliseconds, but how can these be applied to just get the difference between two times in minutes? For example, the difference between 2:45pm and 11:00am is 225 minutes.
You can work out the math by observing that one minute is sixty seconds, one second is one thousand milliseconds, so one minute is 60*1000
milliseconds.
If you divide milliseconds by 60,000, seconds will be truncated. You should divide the number by 1000 to truncate milliseconds, then take n % 60
as the number of seconds and n / 60
as the number of minutes:
Date d1 = ...
Date d2 = ...
long diffMs = d1.getTime() - d2.getTime();
long diffSec = diffMs / 1000;
long min = diffSec / 60;
long sec = diffSec % 60;
System.out.println("The difference is "+min+" minutes and "+sec+" seconds.");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With