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.
Still wasn’t able to find how to set up a future date/time but the following code works well when using today’s date/time:
Date today = new Date()
String todaysDate = today.format(‘mm/dd/yy’)
String nowTime = today.format(‘hh:mm a’)
println(todaysDate)
println (nowTime)
// The following worked for me. Note that I’m using local variables - which have to match the variable names used below.
import groovy.time.TimeCategory as TimeCategory
// I defined the following as local variables “Today1String”, “TodayPlus1Week”
use(groovy.time.TimeCategory, {
def dateFormat = ‘dd MMM yyyy’
def Today1 = new Date()
Today1String = Today1.format(dateFormat)
def TodayPlus1W = Today1 + 7.days
TodayPlus1Week = TodayPlus1W.format(dateFormat)
we can now add and subtract days with the plus() and minus() methods. And because this methods are mapped to the operators + and - we can write dense code.
So … subtract 2*356 from the current date and you are two years back