Get days between two dates

Hi,
I am trying to get count of days between two dates. I have been struggling with this for a while trying to google the solution but no luck. I tried with ChronoUnit but that did not work.

Currently my code looks like this

WebUI.openBrowser(’’)

WebUI.maximizeWindow()

WebUI.navigateToUrl(‘URL’)

String prvniDatum = WebUI.getText(findTestObject(‘LM/FirstDate’))

Date prvniDatumReal = Date.parse(“dd.MM.yyyy”, prvniDatum)

String posledniDatum = WebUI.getText(findTestObject(‘LM/LastDate’))

Date posledniDatumReal = Date.parse(“dd.MM.yyyy”, posledniDatum)

Date today = new Date()

Period period = Period.between(today, posledniDatumReal)
int difference = period.getDays()
print(difference)

The error looks like this :
Reason:

groovy.lang.MissingMethodException: No signature of method: static java.time.Period.between() is applicable for argument types: (java.util.Date, java.util.Date) values: [Tue Sep 29 14:37:18 CEST 2020, Tue Oct 13 00:00:00 CEST 2020]
Possible solutions: between(java.time.LocalDate, java.time.LocalDate)
at LMdatum.run(LMdatum:38)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:339)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:330)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:309)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:301)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:235)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1601383027081.run(TempTestCase1601383027081.groovy:25)

If I understand correctly what it means that I am not providing two Date data types? I am not sure what is the problem since the dates are parsed from string into regular date but it does not work. Also in the error message u can see that the dates actually look like dates?

Thanks to anyone who can enlighten me here since I have been trying to solve this almost whole day.

in this example, you are trying to use a java.util.Date object for a function that uses java.time.LocalDate
if you otherwise don’t need the Date object, i recommend using LocalDate everywhere
here’s how to parse a string
public static LocalDate parseDate(String date, String format=“dd.MM.yyyy”){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
LocalDate result = LocalDate.parse(date, formatter);
return result
}

Date today would become LocalDate today=LocalDate.now()

these two should give you everything you need to use the Period.between function correctly

1 Like

Thanks for the reply at least now I understand it from the theory.
I still cant figure out how to parse the string into date now. I see the function you posted but I am not too familiar with java so I dont know what to do with it. I pasted it in my code but thats about it.
Whenever I try to convert it with

prvniDatumReal = LocalDate.parse(prvniDatum, “dd.MM.yyyy”)

I am getting this error

Text ‘02.10.2020’ could not be parsed at index 0

try LocalDate.parse(prvniDatum, DateTimeFormatter.ofPattern(“dd.MM.yyyy”))

for my testing i have a class where i put commonly used functions for my scenarios, actually using a folder under keywords for that, so it would be

import java.time.format.DateTimeFormatter
import java.time.LocalDate
class Functions{
public static LocalDate parseDate(String date, String format=“dd.MM.yyyy”){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
LocalDate result = LocalDate.parse(date, formatter);
return result
}
}

and then in your testcase
LocalDate prvniDatumReal = Functions.parseDate(prvniDatum, “dd.MM.yyyy”);

1 Like

nice, thank you so much now it is working finally! new class with functions sounds good might do that in the future but for now I am trying to get familiar with katalon

also this code returns 0 for some reason - the dates were 29.9.2020, 29.10.2020

Period period = Period.between(today, posledniDatumReal)
int difference = period.getDays()
print(difference)

Prolly does not matter now since the dates are almost always different and I can figure this out later. Thanks for the help!