在 Java/Groovy 中计算经过的时间 [英] Calculate elapsed time in Java / Groovy
问题描述
我有...
Date start = new Date()
...
...
...
Date stop = new Date()
我想获得这两个日期之间经过的年、月、日、小时、分钟和秒.
I'd like to get the years, months, days, hours, minutes and seconds ellapsed between these two dates.
--
我会细化这个问题.
我只想得到经过的时间,作为一个绝对的度量,即不考虑闰年、每个月的天数等.
I just want to get the elapsed time, as an absolute measure, that is without taking into account leap years, the days of each month, etc.
因此我认为不可能过去几年和几个月,我只能得到几天、几小时、几分钟和几秒钟.
Thus I think it's impossible to get the years and months elapsed, all I can get is days, hours, minutes and seconds.
更具体地说,我想说明某项任务持续了例如
More specifically I want to tell that a certain task lasted for e.g.
20 sec
13 min, 4 sec
2 h, 10 min, 2 sec
4 d, 4 h, 2 min, 2 sec
所以请原谅我不够精确.
So please forgive my lack of precision.
推荐答案
我刚刚发现了这个源自 Groovy 的快速解决方案:
I've just discovered this quick Groovy-sourced solution:
import groovy.time.TimeCategory
import groovy.time.TimeDuration
Date start = new Date()
// do something here
Date stop = new Date()
TimeDuration td = TimeCategory.minus( stop, start )
println td
这篇关于在 Java/Groovy 中计算经过的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!