Groovy中计算已用时间

Groovy中计算已用时间

本文介绍了在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

所以请原谅我缺乏精确度。 / p>

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

TimeDuration td = TimeCategory.minus( stop, start )
println td

这篇关于在Java / Groovy中计算已用时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:20