Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Date 在Groovy中为SimpleDataFormat添加时间_Date_Datetime_Groovy_Soapui_Simpledateformat - Fatal编程技术网

Date 在Groovy中为SimpleDataFormat添加时间

Date 在Groovy中为SimpleDataFormat添加时间,date,datetime,groovy,soapui,simpledateformat,Date,Datetime,Groovy,Soapui,Simpledateformat,我试图向groovy参数添加时间,该参数的DateTime存储在SimpleDataFormat中 import groovy.time.TimeCategory import java.text.SimpleDateFormat def testCase = messageExchange.modelItem.testCase; def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").t

我试图向groovy参数添加时间,该参数的DateTime存储在
SimpleDataFormat

import groovy.time.TimeCategory
import java.text.SimpleDateFormat 
def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
log.info startdatetime
aaa =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(startdatetime)
use(TimeCategory) 
{
    def enddatetime = aaa + 5.minutes
    log.info enddatetime
}
开始日期:星期三11月8日19:57:50 IST 2017:信息:2017-11-08T15:00:00.000Z

错误弹出窗口显示消息

'不可解析日期:“2017-11-08T15:00:00.000Z”'


您可能需要
“yyyy-MM-dd'HH:MM:ss'Z'”
而不是
“yyy-MM-dd'HH:MM:ss.SSS'Z'”
,因为您的输入字符串包括毫秒。

而不是
“yyy-MM-dd'HH:MM:ss'Z'”
您可能需要
“yyyy-MM-dd'HH:MM:ss'Z'”
因为您的输入字符串包含毫秒。

我没有使用Groovy的经验,但我认为既然您可以使用Java类,也可以使用现代Java日期和时间API。我强烈建议在长期过时的
SimpleDateFormat
类中使用。您的格式,
2017-11-08T15:00:00.000Z
,与
SimpleDateFormat
没有任何联系,相反,它是ISO 8601,现代日期和时间类(与旧日期和时间类相反)本机“理解”的格式,无需显式格式化程序进行解析

因此,我建议您尝试(绝不测试):

还有可能(如果您仍然需要或想要使用Java类)


我没有使用Groovy的经验,但我认为既然可以使用Java类,也可以使用现代Java日期和时间API。我强烈建议在长期过时的
SimpleDateFormat
类中使用。您的格式,
2017-11-08T15:00:00.000Z
,与
SimpleDateFormat
没有任何联系,相反,它是ISO 8601,现代日期和时间类(与旧日期和时间类相反)本机“理解”的格式,无需显式格式化程序进行解析

因此,我建议您尝试(绝不测试):

还有可能(如果您仍然需要或想要使用Java类)


如果日期字符串是
Wed Nov 08 19:57:50 IST 2017
,并且希望将其转换为日期对象,则可以执行以下操作:

def dateString = "Wed Nov 08 19:57:50 IST 2017"
def dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
def date = Date.parse(dateFormat, dateString)
看起来你想增加5分钟,这可以像以前一样完成

def endDate
use(TimeCategory) { endDate = date + 5.minutes }
log.info "End date : $endDate"
如果要格式化日期对象,请执行以下操作:

def outputDateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
log.info "Formatted date: ${date.format(outputDateFormat)}"
另一个建议是,在查看代码以获取项目属性值后,使用下面的一行

更改自:

def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
def startDateTime = context.expand('${#Project#StartDateTime}')
至:

def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
def startDateTime = context.expand('${#Project#StartDateTime}')

如果日期字符串是
Wed Nov 08 19:57:50 IST 2017
,并且希望将其转换为日期对象,则可以执行以下操作:

def dateString = "Wed Nov 08 19:57:50 IST 2017"
def dateFormat = "EEE MMM dd HH:mm:ss Z yyyy"
def date = Date.parse(dateFormat, dateString)
看起来你想增加5分钟,这可以像以前一样完成

def endDate
use(TimeCategory) { endDate = date + 5.minutes }
log.info "End date : $endDate"
如果要格式化日期对象,请执行以下操作:

def outputDateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
log.info "Formatted date: ${date.format(outputDateFormat)}"
另一个建议是,在查看代码以获取项目属性值后,使用下面的一行

更改自:

def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
def startDateTime = context.expand('${#Project#StartDateTime}')
至:

def testCase = messageExchange.modelItem.testCase;
def startdatetime = testCase.testSuite.project.getPropertyValue("StartDateTime").toString();
def startDateTime = context.expand('${#Project#StartDateTime}')

靠近“2017-11-08T15:00:00.000Z”结尾的
.000
是毫秒。靠近“2017-11-08T15:00:00.000Z”结尾的
.000
是毫秒。感谢您的回答和建议。感谢您的回答和建议。