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

4

2 回答 2

3

请参阅如何将毫秒转换为 x-mins-x-seconds-in-java

然后你可以做这样的事情......

final Date a = someDate();
final Date b = anotherDate();
final long millis = b.getTime() - a.getTime();

int minutes = TimeUnit.MILLISECONDS.toMinutes(millis);

另外,在某个时候看看Joda。它几乎是 Java 中更好的日期和时间函数的事实标准。他们有一些很好的方便方法,比如Minutes.minutesBetween(date1,date2)

于 2011-07-07T19:51:28.460 回答
-1

只需创建两个您想要的日期戳并将其转换为毫秒并减去它们并返回差异。

Date date1 = System.currentMilliSeconds();
Date date2 = System.currentMilliSeconds();
return new Date(date1.getTime() - date2.getTime());
于 2011-07-07T19:54:43.357 回答