datetime - 提取日期时间,然后与之前的日期时间进行比较
问题描述
我想确保存储在数据库表中的日期时间比之前捕获的日期时间不超过 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'
解决方案
结果是地图列表。要使该检查起作用,您必须将其编写为:
assert resultsAfter.first().col1 < date
这仅适用于第一个结果,并且只有当有一个结果时才有效。假设,您想要断言对于所有元素,您可以使用every
或循环结果并对每一行执行断言。
然而,在这一点上,我只是让数据库做这项工作:选择所有不符合标准的项目,并确保没有找到任何结果。
推荐阅读
- python - Pipenv 从哪里获取 CVE 数据?
- c++ - c++ 中的 range-for 会调用项目的复制构造函数吗?
- php - jquery 和删除类不起作用
- sql - SQL Server 2016:获取前 3 个逗号分隔值
- mysql - 无法使用 MySQL(8.0.15) (ER_ACCESS_DENIED_ERROR) 验证我的 Node js(v10.15.3) 应用程序、npm mysql (v2.17.1)
- mysql - 如何将 MySQL 数据库与 Jenkins 集成
- fortran - Fortran 中的多维数组
- javascript - 如何制作适用于 IOS 和桌面浏览器的自定义全屏视频按钮?
- r - 如何删除满足多个条件的重复行
- python - Python 帐户检查器:检查问题