Search code examples
javaparsingdatesimpledateformatmilliseconds

Java determine if a time is X minutes greater


I know how to determine IF a time stamp is "greater than" "equal to" or "less than" using the date function, but I don't know how to determine HOW MUCH greater than a time is

for instance, how would I tell that 6:15 PM is 15 minutes greater than 6:00 PM

I've got an idea to convert times into milliseconds and compare with the current time in milliseconds, but its a fog of potential ideas right now

insight appreciated


Solution

  • see how-to-convert-milliseconds-to-x-mins-x-seconds-in-java

    Then you can do something like this...

    final Date a = someDate();
    final Date b = anotherDate();
    final long millis = b.getTime() - a.getTime();
    
    int minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    

    Also, take a look at Joda at some point. It's pretty much the de-facto standard for better date and time functions in java. They have some nice convenience methods like Minutes.minutesBetween(date1,date2)