开发者

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开发者_Go百科 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


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)


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());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜