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

Convert current time(IST) to UTC using groovy script in SoapUI

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

我尝试了以下代码:

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 分钟后的时间。

使用 SimpleDateFormatter 您可以设置 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)}")

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

println new Date().format("yyyy-MM-dd HH:mm:ss", TimeZone.getTimeZone('UTC'))

当然,您可以根据需要更改所需的日期格式,因为您没有提到。

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

说,请求中有date个元素

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

编辑:基于操作评论;需要获取延迟时间(从现在起10分钟后)

<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>

以上应该有效。但是,SoapUI 似乎无法解析上述内容。 解决方法是在请求步骤之前使用 groovy 脚本测试步骤,如下所示:

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)

在请求正文中,使用<date>${#TestCase#FUTURE_TIME}</date>