两分钟之间的差异 [英] Difference between two times in minutes

查看:107
本文介绍了两分钟之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到一些使用Joda Time和其他方法的例子来计算两个日期之间的差异(以毫秒为单位),但是这些如何应用于在几分钟内得到两次之间的差异?例如,下午2:45到11:00之间的差异是225分钟。

I've seen some examples using Joda Time and other methods to work out the difference between two dates in milliseconds, but how can these be applied to just get the difference between two times in minutes? For example, the difference between 2:45pm and 11:00am is 225 minutes.

推荐答案

您可以通过观察计算出数学一分钟是六十秒,一秒钟是一千毫秒,所以一分钟是 60 * 1000 毫秒。

You can work out the math by observing that one minute is sixty seconds, one second is one thousand milliseconds, so one minute is 60*1000 milliseconds.

如果将毫秒除以60,000,则秒将被截断。您应该将数字除以1000以截断毫秒,然后将 n%60 作为秒数并且 n / 60 作为分钟数:

If you divide milliseconds by 60,000, seconds will be truncated. You should divide the number by 1000 to truncate milliseconds, then take n % 60 as the number of seconds and n / 60 as the number of minutes:

Date d1 = ...
Date d2 = ...
long diffMs = d1.getTime() - d2.getTime();
long diffSec = diffMs / 1000;
long min = diffSec / 60;
long sec = diffSec % 60;
System.out.println("The difference is "+min+" minutes and "+sec+" seconds.");

这篇关于两分钟之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆