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!

I have a slightly different scenario now. Do you know how I can modify this calculation to have the date be at least 10 years ago or older?

Try the lower limit to upper limit concept on the below site, so 10 years as lower limit may be 3650.

all you really have to do is to modify
LocalDate date=LocalDate.now()
to
LocalDate date=LocalDate.now().minusYears(10)
or, as a keyword

@Keyword
public static String getDateStringForDateBefore(String format=“dd/MM/YYY”, int yearoffset=0){
int maxDayDifference=1000
LocalDate date=LocalDate.now().minusYears(yearoffset)
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
}

I use the following Custom Keyword …

  1. Create a keyword as follows:
package tools
import java.text.SimpleDateFormat as SimpleDateFormat

public class ProjectDates {
	def String VerDate() {
		//Creates current date
		//Available date formats: MMddyyyyHHmmssSSS
		SimpleDateFormat sdf = new SimpleDateFormat('MMddyyyy')
		Calendar c = Calendar.getInstance()
		c.setTime(new Date())
		c.add(Calendar.DATE, 0)
		c.add(Calendar.YEAR, 0)
		def VerDate = sdf.format(c.getTime())
		return VerDate
	}
	def String FutureDate() {
		//Creates 5 year expiration date
		//Available date formats: MMddyyyyHHmmssSSS
		SimpleDateFormat sdf = new SimpleDateFormat('MMddyyyy')
		Calendar c = Calendar.getInstance()
		c.setTime(new Date())
		c.add(Calendar.DATE, 0)
		c.add(Calendar.YEAR, 5)
		def FutureDate = sdf.format(c.getTime())
		return FutureDate
	}
	def String BirthDate() {
		//Creates 20 year old birth date
		//Available date formats: MMddyyyyHHmmssSSS
		SimpleDateFormat sdf = new SimpleDateFormat('MMddyyyy')
		Calendar c = Calendar.getInstance()
		c.setTime(new Date())
		c.add(Calendar.DATE, 0)
		c.add(Calendar.YEAR, -20)
		def BirthDate = sdf.format(c.getTime())
		return BirthDate
	}
}
  1. Create a new test case as follows:
    Create new test case
    Switch to Script view
    Paste the following to the body of the test case
    Run the test case
    Switch to Console view to see the results

Script view:

def VerDate = CustomKeywords.'tools.ProjectDates.VerDate'()
println('VerDate : ' + VerDate )

def FutureDate = CustomKeywords.'tools.ProjectDates.FutureDate'()
println('FutureDate: ' + FutureDate)

def BirthDate = CustomKeywords.'tools.ProjectDates.BirthDate'()
println('BirthDate: ' + BirthDate)

Console view:

2020-12-08 12:06:19.731 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2020-12-08 12:06:19.733 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/05 How_to_cases/25 Katalon_tips/00 How-to use ProjectDates Keyword
2020-12-08 12:06:19.918 DEBUG t.00 How-to use ProjectDates Keyword     - 1: VerDate = tools.ProjectDates.VerDate()
2020-12-08 12:06:19.938 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - tools.ProjectDates.VerDate is PASSED
2020-12-08 12:06:19.938 DEBUG t.00 How-to use ProjectDates Keyword     - 2: println("VerDate: " + VerDate)
VerDate: 12082020
2020-12-08 12:06:19.941 DEBUG t.00 How-to use ProjectDates Keyword     - 3: FutureDate = tools.ProjectDates.FutureDate()
2020-12-08 12:06:19.942 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - tools.ProjectDates.FutureDate is PASSED
2020-12-08 12:06:19.943 DEBUG t.00 How-to use ProjectDates Keyword     - 4: println("FutureDate: " + FutureDate)
FutureDate: 12082025
2020-12-08 12:06:19.944 DEBUG t.00 How-to use ProjectDates Keyword     - 5: BirthDate = tools.ProjectDates.BirthDate()
2020-12-08 12:06:19.945 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - tools.ProjectDates.BirthDate is PASSED
2020-12-08 12:06:19.945 DEBUG t.00 How-to use ProjectDates Keyword     - 6: println("BirthDate: " + BirthDate)
BirthDate: 12082000
2020-12-08 12:06:19.947 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/05 How_to_cases/25 Katalon_tips/00 How-to use ProjectDates Keyword

Thanks! That worked.