如何将字符串转换为日期并在转换回字符串之前执行操作?

How to convert string to date and perform actions before converting back to string?

关于将字符串 属性 值设置为日期格式,执行日期操作然后将 属性 值恢复为字符串的小问题。

计划是我有一个名为 testRunner.testCase.setPropertyValue( "LastModifiedDateTo") 的 属性 变量是测试 2。目前它的值为 20170203 但这可以是任何动态值。

我想做的是如果if语句条件为真:

下面我只是在 if 语句中有一个硬编码值作为我对完成值的要求,但我希望按照上面的方法替换它。有人可以帮助我如何完成转换和设置日期吗?

def test1 = testRunner.testCase.getPropertyValue( "LastModifiedDateTo")
def test2 = testRunner.testCase.getPropertyValue( "LastModifiedTimeFrom")
def test3 = testRunner.testCase.getPropertyValue( "LastModifiedTimeTo")

log.info test1
log.info test2
log.info test3

if (test2 == "23:50:00" && test3 == "00:00:00") {
   testRunner.testCase.setPropertyValue( "LastModifiedDateTo", "20170204")
   log.error "true"
} else {
   log.error "false"
}

日期格式通常无关紧要,您要做的是在给定日期上添加一天。可以通过以下方式获取(with TimeCategory):

import groovy.time.TimeCategory

def date = Date.parse('yyyyMMdd', '20170203')

use(TimeCategory) {
    date = date + 1.day
}

assert date.format('yyyyMMdd') == '20170204'