首页 > 解决方案 > 提取日期时间,然后与之前的日期时间进行比较

问题描述

我想确保存储在数据库表中的日期时间比之前捕获的日期时间不超过 2 分钟。

从数据库表返回的结果就是这种格式。

[[col1:2020-05-28 04:02:21.34]]

我的代码

import java.text.SimpleDateFormat
//capture current date time
def date = new Date()
println date.format('yyyy-MM-dd hh:mm:ss.SS',TimeZone.getTimeZone('UTC'))

//wait 2 minutes then capture DB table date time
WebUI.delay(120)
PostgresdbQuery = /SELECT col1 FROM table1.test/
List resultsafter = CustomKeywords.'test.database.getPostgresSQLResults'(GlobalVariable.testPostgresdbConnString , GlobalVariable.testPostgresdbUsername , GlobalVariable.testPostgresdbPassword ,GlobalVariable.testPostgresdbDriver ,PostgresdbQuery )
println(resultsafter)

//assert 
assert resultsafter < date, 'Execute time is within 2 minutes'

错误

Reason:
groovy.lang.GroovyRuntimeException: Cannot compare java.util.ArrayList with value '[{col1=2020-05-28 04:02:21.34}]' and java.util.Date with value '5/28/20 1:49 PM'

标签: datetimegroovy

解决方案


结果是地图列表。要使该检查起作用,您必须将其编写为:

assert resultsAfter.first().col1 < date

这仅适用于第一个结果,并且只有当有一个结果时才有效。假设,您想要断言对于所有元素,您可以使用every或循环结果并对每一行执行断言。

然而,在这一点上,我只是让数据库做这项工作:选择所有不符合标准的项目,确保没有找到任何结果。


推荐阅读