在SoapUI中使用groovy脚本将当前时间(IST)转换为UTC

在SoapUI中使用groovy脚本将当前时间(IST)转换为UTC,groovy,soapui,Groovy,Soapui,我是Groovy脚本的新手,我正在soapUI中寻找Groovy脚本,在将当前时间(IST)传递给API之前,我可以将其转换为UTC 我尝试了以下代码: import java.util.*; def d = new Date() def CurrentDay = ('0' + d.getUTCDate()).slice(-2) def CurrentMonth = ('0' + d.getUTCMonth()).slice(-2) def CurrentYear = d.getUTCFull

我是Groovy脚本的新手,我正在soapUI中寻找Groovy脚本,在将当前时间(IST)传递给API之前,我可以将其转换为UTC

我尝试了以下代码:

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;
但这对我没有帮助。期待您的帮助


编辑:OP在回答中评论说,他希望未来的时间是10分钟后的时间。

使用
SimpleDataFormatter
可以设置
时区:

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)}")

您可以通过以下简单方式将本地日期转换为不同的时区:

println new Date().format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone('UTC'))
当然,您可以根据需要更改所需的日期格式,因为您没有提到

如果您需要在Soap请求中发送相同的日期,则不需要额外的
Groovy脚本
测试步骤来创建日期。相反,您可以在请求本身中使用内联代码,如下所示:

比如说,请求中有
date
元素

<date>${= new Date().format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone('UTC'))}</date>

在请求正文中,使用
${#TestCase#FUTURE#TIME}

Basu-Raj,你能检查一下解决方案是否有用吗?嘿,Rao,这个信息非常有用,它帮助我优化了代码。谢谢。很高兴你发现它很有用。使用这一行代码,我将获得转换的UTC时间,那么现在如果我想在转换的时间上增加+10分钟?如何做到这一点?当然罗,我的要求是,在将来安排一些任务。为此,我需要选择当前时间(IST)添加额外的15或30分钟,然后将其转换为UST,然后将其传递给API。还有,让我看看如何从请求体传递这个消息?@BasuRaj,你能检查一下答案的编辑部分,看看这是否有用吗?感谢你的回答
<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>
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)