On running the following code in groovy –
import groovy.time.*
import org.codehaus.groovy.runtime.TimeCategory
def today = new Date()
use(TimeCategory)
{
def modifiedToday = today.plus(10.minutes)
modifiedToday = modifiedToday.plus(10.months)
modifiedToday = modifiedToday.plus(10.years)
def duration = modifiedToday - today
println duration.years
println duration.months
println duration.days
println duration.minutes
}
I am getting the following output –
0
0
3956
10
Please suggest, why am I getting years and months as 0 and all the value in days. How do I get the value in years and months?
How would you get it in months?
Each month has a different number of days, so what would you do?
You can get back the date this represents from now by doing:
Or, you can get the date that represents in the past by doing:
And I guess you could work it out from there, but there is no in-built functionality for normalising a TimeDuration based on a given date
Edit
This sort of thing rolls from one date in the past to the specified date. I haven’t done any real testing on it though, so you should take care and test the life out of it before using it in anything important…