Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

date - Calculate elapsed time in Java / Groovy

I have...


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'll refine the question.

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.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...