How to calculate difference between dates

I’ve tried doing:

use(groovy.time.TimeCategory) {
duration = duration + (date1- date2).toMilliseconds() / 1000
}
but I get
java.lang.SecurityException: Invoking methods on class groovy.time.TimeDuration is not allowed

I also tried (date1-date2-).seconds but get a similar message on accessing properties on groovy.time.TimeDuration not being allowed

now().time or your date object.time