Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duration between two dates in Groovy

Tags:

groovy

Is there a way in Groovy to get the duration between two Date objects? The duration format I'm looking for would be something like: 2 days, 10 hours, 30 minutes...

Thanks

like image 931
RyanLynch Avatar asked May 03 '10 02:05

RyanLynch


People also ask

How do I use TimeCategory in groovy?

Using TimeCategory make DateTime manipulation simpler than using regular Java time/date API. Like the previous tutorial, we will use web Groovy executor to run our Groovy codes. Open your browser and go to Groovy Console. Next, just paste your code inside code box then click execute.


2 Answers

TimeCategory has some methods for getting a duration. You could use it like

use(groovy.time.TimeCategory) {     def duration = date1 - date2     print "Days: ${duration.days}, Hours: ${duration.hours}, etc." } 
like image 110
ig0774 Avatar answered Sep 28 '22 08:09

ig0774


The use()-Syntax is weird to me. so I prefer it like this:

def duration = groovy.time.TimeCategory.minus(   new Date(),   new Date(session.creationTime) ); def values = [             "seconds: " + duration.seconds,             "min: " + duration.minutes,             "hours: " + duration.hours,             "days: " + duration.days,             "ago: " + duration.ago, ]; 
like image 37
Fusca Software Avatar answered Sep 28 '22 09:09

Fusca Software