1

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

CQM
  • 42,592
  • 75
  • 224
  • 366

2 Answers2

3

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)

Community
  • 1
  • 1
Michael J. Lee
  • 12,278
  • 3
  • 23
  • 39
-1

Just create two date stamps you want and convert into milliseconds and subract them and return the difference.

Date date1 = System.currentMilliSeconds();
Date date2 = System.currentMilliSeconds();
return new Date(date1.getTime() - date2.getTime());
Nick
  • 1,692
  • 3
  • 21
  • 35