Is there a way to get the current ‘date’ and ‘time’, modify them and use as a new input?


In our case we need to populate a calendar with a future value, say ‘today’s date + 3 days’. Same for the current time. Is there a way to get the current date, modify it and use as an input in our test cases ?

Example: our application accept the following date format 05/17/17 (mm/dd/yy).
If today is 05/17/17 we need to slightly modify that(+ 3 days), so we have 05/20/17 and then populate the input filed with the new value.



You can define a custom keyword which returns the date with an option of adding days to it,

Here is the code:

//Keyword to get the date takes the numeric parameter which is the number of days to be added to the current date(UTC)@Keyword
def returnDate(int addDays){
	DateFormat dateFormat = new SimpleDateFormat("MM/dd/yy");
	dateFormat.setTimeZone(new SimpleTimeZone(SimpleTimeZone.UTC_TIME, "UTC"));
	Date date = addDays(date, +addDays);
	return dateFormat.format(date);				
}//Function that actually adds days to given Date
public static Date addDays(Date date, int days) {
	GregorianCalendar cal = new GregorianCalendar();
	cal.add(Calendar.DATE, days);

In the script,

//date variable. Pass in the number of days you want to add to current date.def dateToBeInputted = CustomKeywords.'packageLibrary.KeywordLibrary.returnDate'(0);

Here is the function to add minutes, which takes the date object and number of minutes to be added as a parameter,

//Function that adds mentioned minutes to given Date
public static Date addMinutes(Date date, int minutes) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.add(Calendar.MINUTE, minutes);
        return cal.getTime();

Let me know if that doesn’t work out,

Thank you

This is the code I am using.

time_started = new Date()
println "TEST STARTED AT (America/New_York): " + time_started.format(“yyyyMMdd-HH-mm”, TimeZone.getTimeZone(‘America/New_York’))
runtime = " "+ time_started.format(“yyyyMMddHHmm”, TimeZone.getTimeZone(‘America/New_York’))
seconds = time_started.format(“HHmmss”, TimeZone.getTimeZone(‘America/New_York’))

Maybe this is something you can use to calculate minutes, hours, days, month years (seconds I did not need for my purpose): I created this keyword function for an outsystems specific datepicker to calculate based on a datetime value.

Calling code in testscript

String strDateTimenow
strDatenow = ‘20181226’
strTimenow = ‘05:32’

//concatenate the value as i
strDateTimenow = strDatenow + ’ ’ + strTimenow
String strDatenew = ‘’
String strTimenew = ‘’
String strDateTimenew
//Call the custom function to calculate the dates
strDateTimenew = CustomKeywords.‘utility.keywords.ChangeDates.changedatetime’(5, 30, 0, 0, 0, strDateTimenow)
// parameters
// 1. minutes – int
// 2. hours – int
// 3 days – int
// 4 months – int
// 5 years – int
// datetime you just entered and concatenated, make sure the simeple date format of the customfunction
// in this case “yyyyMMdd HH:mm”

package utility.keywords

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 com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testcase.TestCaseFactory
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testdata.TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

import internal.GlobalVariable
import java.text.SimpleDateFormat
import MobileBuiltInKeywords as Mobile
import WSBuiltInKeywords as WS
import WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.interactions.Action
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.WebDriver
import groovy.time.TimeCategory
import com.kms.katalon.core.logging.KeywordLogger
import java.util.Date

public class ChangeDates {
private def Today

def changedatetime(int inp_MinutefromToday, int inp_HourfromToday, int inp_DatefromToday, int inp_MonthfromToday,int inp_YearfromToday,String strDatenowfromDatePicker){
	String newDate = strDatenowfromDatePicker
	def Todayd = new SimpleDateFormat("yyyyMMdd HH:mm")
	Date Today =  Todayd.parse(newDate)
	use (groovy.time.TimeCategory){
		//	KeywordLogger log = new KeywordLogger()
		//def today = input.parse("MM-dd-yyyy", startTime)
         Date out_DatefromToday = Today

		out_DatefromToday = Today + inp_MinutefromToday.minutes + inp_HourfromToday.hours + inp_DatefromToday.days + inp_MonthfromToday.months + inp_YearfromToday.years
		String Currentdate = out_DatefromToday.toString()
		def Currentdateparse = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH)
		def CurrentDateAdjusted = Currentdateparse.parse(Currentdate)
		def sdff = new SimpleDateFormat("yyyyMMdd HH:mm")
		def frmDate = sdff.format(CurrentDateAdjusted); // Handle the ParseException here

		return frmDate




on returning of the function you can substring the datenew and timenew
strDatenew = strDateTimenew.substring(0,8)
strTimenew = strDateTimenew.substring(9,14)

good luck

Try this:

//Add to the import section
import java.text.SimpleDateFormat

//Add to the body of test case
SimpleDateFormat sdf = new SimpleDateFormat(“MM/dd/yyyy”);
Calendar c = Calendar.getInstance();
c.setTime(new Date()); //Use today’s date
c.add(Calendar.DATE, 2); //Adds # of days
c.add(Calendar.YEAR, 2); //Adds years
String currentDate = sdf.format(c.getTime());
System.out.println("Future Date (MM/DD/YYYY): " + currentDate);

Time examples can be found here:

1 Like