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
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.
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." }
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, ];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With