Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to compare Time in Java [duplicate]

I need to compare two times in java and make sure they are within ten minutes from each other. This would seem simple enough and was simple enough before Date was deprecated. Does anyone have any idea of how the best way to program this in java is? I saw the gregorian calendar but i dont see how to access the fields for minutes or day etc.

like image 383
auwall12688 Avatar asked Mar 20 '26 14:03

auwall12688


1 Answers

Date.getTime() is not deprecated and gives you a long representing milliseconds since the epoch. You can use this as basis for such simple comparisons.

long diffInMins = Math.abs(date1.getTime() - date2.getTime()) / 60000;
like image 84
sudocode Avatar answered Mar 23 '26 04:03

sudocode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!