Reputation: 105
I am new to Groovy script and I am looking for a groovy script in soapUI, where I can convert my current time (IST) to UTC before passing it to API.
I tried below code:
import java.util.*;
def d = new Date()
def CurrentDay = ('0' + d.getUTCDate()).slice(-2)
def CurrentMonth = ('0' + d.getUTCMonth()).slice(-2)
def CurrentYear = d.getUTCFullYear()
def CurrentHours = ('0' + d.getUTCHours()).slice(-2)
def CurrentMinutes = ('0' + d.getUTCMinutes()).slice(-2)
def CurrentSeconds = ('0' + d.getUTCSeconds()).slice(-2)
def DateFormatted = CurrentYear.toString() + CurrentMonth.toString() + CurrentDay.toString() + CurrentHours.toString() + CurrentMinutes.toString() + CurrentSeconds.toString()
return DateFormatted;
But it's not helping me. Looking forward for the help.
EDIT: OP commented in the answer that he wanted to have future time say 10 minutes later time.
Upvotes: 2
Views: 8561
Reputation: 21379
You can convert the local date to different time zone in simple way:
println new Date().format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone('UTC'))
Of course, you can change the required date format as needed since you haven't mentioned.
In case if you need to send the same date in the Soap request, you do not have to have an additional Groovy Script
test step to create the date. Instead, you can just use in-line code in the request itself as shown below:
Say, there is date
element in the request
<date>${= new Date().format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone('UTC'))}</date>
EDIT: Based on op comments; need to get the delayed time (10 minutes later from now)
<date>${= def date=new Date(); use(groovy.time.TimeCategory){date = date.plus(10.minutes)}; date.format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone('UTC'))}</date>
The above should work. However, SoapUI seems to have issue parsing the above. The workaround is to use a groovy script test step before request step as shown below :
def date=new Date()
use(groovy.time.TimeCategory){ date = date.plus(10.minutes) }
def dateString = date.format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone('UTC'))
context.testCase.setPropertyValue('FUTURE_TIME', dateString)
In the request body, use <date>${#TestCase#FUTURE_TIME}</date>
Upvotes: 4
Reputation: 84784
With SimpleDateFormatter
you can set the TimeZone
:
import java.text.SimpleDateFormat;
def d = new Date()
def sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss")
sdf.setTimeZone(TimeZone.getTimeZone("IST"))
println("IST ${sdf.format(d)}")
sdf.setTimeZone(TimeZone.getTimeZone("UTC"))
println("UTC ${sdf.format(d)}")
Upvotes: 1