Get date duration difference in days

Hello,

I want to get duration in days, when I try to calculate date duration between date 1 and date 2.

This is how I am trying to do:

def today = new Date().format('dd/MM/yy)
def calcDate = "13/06/18"

use(groovy.time.TimeCategory) {
def duration = today - calcDate
print "Days: ${duration.days}"
}

I get the error message, when I execute this script.

Reason:
groovy.lang.MissingPropertyException: No such property: days for class: java.lang.String
Possible solutions: class

I’ve also tried this instead of use()…

def summary = TimeCategory.minus(today, calcDate)
println summary

On this I got an error too :

Rreason:
groovy.lang.MissingMethodException: No signature of method: static groovy.time.TimeCategory.minus() is applicable for argument types: (java.lang.String, java.lang.String) values: [02/08/19, 13/06/18]

Hi,

I don’t know this API, but I have a solution using LocalDate and ChronoUnit.

import java.time.LocalDate
import java.time.temporal.ChronoUnit

LocalDate d1 = LocalDate.now()
LocalDate d2 = d1.plusDays(10)

println ChronoUnit.DAYS.between(d1, d2)
2 Likes

calcDate is a string.
use @Marek_Melocik solution or check into the Date class documentation how to create a datetime object with a specific value.
in order to do substitution on them, both objects has to be of same type

this may help: https://www.tutorialspoint.com/groovy/groovy_dates_times

1 Like

Thank you very much