Random Date Generation

Does anyone have any good ways of generating a random date in the past in the form of dd/MM/YYY?

import java.time.LocalDate
import java.time.format.DateTimeFormatter
import com.kms.katalon.core.annotation.Keyword
import java.util.Random
class Functions{
    @Keyword
    public static LocalDate getDateBefore(LocalDate date=LocalDate.now(), int maxDayDifference=1000){
        	Random rand = new Random()
        	int days = rand.nextInt(maxDayDifference)
        	return date.minusDays(days)
        }

    @Keyword
    public static String getDateString(LocalDate date, String format="dd/MM/YYY"){
        	DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
        	String formattedDateTime = date.format(formatter);
        	return formattedDateTime
        }
}

these keywords should go into a .groovy file under keywords
and then, while editing a testcase, they should show up under ‘+add > Custom Keyword’

Does this get placed in a keyword? If so, how is that called?

And, what libraries I need to import for this?

i’ve edited the original answer with the requested information, hope everything is clear now

Thank you! The clarity is getting there, but still a bit confused. I tried calling this keyword with the following statement:

println(CustomKeywords.‘com.patientinfo.DateofBirth.getDateString’())

I receive the following error:

=============== ROOT CAUSE =====================

For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshoot-common-execution-exceptions-web-test.html

11-19-2020 09:39:39 AM Test Cases/Drug Dashboard/Patient List/Conductor/Conductor Add New Patient

Elapsed time: 0.617s

com.patientinfo.DateofBirth.invokeMethod:0

Test Cases/Drug Dashboard/Patient List/Conductor/Conductor Add New Patient FAILED.
Reason:
org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method getDateString() to invoke from this list:
public static java.lang.String com.patientinfo.DateofBirth#getDateString(java.time.LocalDate)
public static java.lang.String com.patientinfo.DateofBirth#getDateString(java.time.LocalDate, java.lang.String)
at com.patientinfo.DateofBirth.invokeMethod(DateofBirth.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
at Conductor Add New Patient.run(Conductor Add New Patient:25)
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 TempTestCase1605800377309.run(TempTestCase1605800377309.groovy:25)

you need to pass the date to the method you’re calling, you can store the result from getDateBefore as a globalvariable or you can combine the two methods if you don’t need to reuse them, here’s a simplified version

@Keyword
public static String getDateStringForDateBefore(String format="dd/MM/YYY"){
int maxDayDifference=1000
LocalDate date=LocalDate.now()
Random rand = new Random()
int days = rand.nextInt(maxDayDifference)
 LocalDate randomDate=date.minusDays(days)
 DateTimeFormatter formatter = DateTimeFormatter.ofPattern(format);
 String formattedDateTime = randomDate.format(formatter);
 return formattedDateTime
}

Thank you! This is exactly what I am looking for!