Groovy date picker adds incorrect date value

Help Please
In my test cases I need to select time in future which will be different each time the test case is run.
Essentially to schedule event start time, announcement start time in the future

To achieve this we have create a groovy script as follows:

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint

import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase

import static com.kms.katalon.core.testdata.TestDataFactory.findTestData

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint

import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW

import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile

import com.kms.katalon.core.model.FailureHandling as FailureHandling

import com.kms.katalon.core.testcase.TestCase as TestCase

import com.kms.katalon.core.testdata.TestData as TestData

import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW

import com.kms.katalon.core.testobject.TestObject as TestObject

import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows

import internal.GlobalVariable as GlobalVariable

import org.openqa.selenium.Keys as Keys

import groovy.time.TimeCategory as TimeCategory

def desiredDate = new Date()

use(TimeCategory, {

desiredDate = (desiredDate + 2.hours)

})

def formattedDesiredDate = desiredDate.format(‘dd-MMM-yyyy HH:mm’)

with the following package:
package medapps

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows

import internal.GlobalVariable

public class TimeHourAgo {
}

the test case fails as the date that is entered looks like this 62/02/3125 due to this obscure date the test fails to continue to the next step. Clearly I am missing something from my script what do I need to add to get a the desired current date and time plus 2 hours so the result will be 06/06/2023 10.55am, inspecting the time looks like this

Do you stick to groovy.time.TimeCategory class?

I personally have never used groovy.time.TimeCategory. I read an Baeldung article about it and found I do not like it. The groovy.time.TimeCategory class work on top of the old java.util.Date class. I do no like to use java.util.Date any longer. And I have a doubt about the behavior of TimeCategory, as I found the following post

I always use the java.time package of Java8. It is cleanly designed. I am contented with it. I find no reason to choose other date/time library.

A sample Test Case script using Java8 Date/Time API:

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd hh:mm")

LocalDateTime currentDateTime = LocalDateTime.now()
println "currentDateTime= " + currentDateTime.format(dtf);

LocalDateTime plus2hours = currentDateTime.plusHours(2)
println "plus 2 hours   = " + plus2hours.format(dtf)

output:

2023-06-06 10:35:45.241 INFO  c.k.katalon.core.main.TestCaseExecutor   - --------------------
2023-06-06 10:35:45.244 INFO  c.k.katalon.core.main.TestCaseExecutor   - START Test Cases/currentDatePlus2hours
currentDateTime= 2023/06/06 10:35
plus 2 hours   = 2023/06/06 12:35
2023-06-06 10:35:46.217 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/currentDatePlus2hours

Hi Kazurayam
thank you, as I am not a developer and extremely new to Katalon studio with the script you have provided would I just paste that at the start of my test case?

Why not you just try?