groovy - 升级到 SoapUI Pro 3.3.2 - 日期问题
问题描述
我将 SoapUI Pro 升级到 3.3.2 版,现在我遇到了日期格式问题。当我运行此代码时:
def startDate = new Date()
def logg = startDate.format("HH:mm:ss.S", TimeZone.getTimeZone('CET'))
此错误引发:
groovy.lang.MissingMethodException:没有方法签名:java.util.Date.format() 适用于参数类型:(String, sun.util.calendar.ZoneInfo) 值:[HH:mm:ss.S, sun。 util.calendar.ZoneInfo[id="CET",offset=3600000,dstSavings=3600000,useDaylight=true,transitions=137,lastRule=java.util.SimpleTimeZone[id=CET,offset=3600000,dstSavings=3600000,useDaylight= true,startYear=0,startMode=2,startMonth=2,startDay=-1,startDayOfWeek=1,startTime=7200000,startTimeMode=1,endMode=2,endMonth=9,endDay=-1,endDayOfWeek=1,endTime= 7200000,endTimeMode=1]]] 可能的解决方案:stream(), toYear(), from(java.time.Instant) 错误在第 15 行
我究竟做错了什么?
解决方案
您应该可以访问java.time
通常优于java.util.Date
. 如果对您的其余工作来说不是太麻烦,我建议您切换。
如果您必须使用java.util.Date
,您将需要一个 SimpleDateFormat:
import java.text.SimpleDateFormat
sdf = new SimpleDateFormat("HH:mm:ss.S")
sdf.setTimeZone(TimeZone.getTimeZone("CET"))
println sdf.format(new Date())
==> 06:01:31.299
使用ZonedDateTime
from java.time
:
import java.time.ZoneId
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
i = ZonedDateTime.now(ZoneId.of("CET"))
println i.format(DateTimeFormatter.ofPattern("HH:mm:ss.S"))
println i.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"))
==> 06:01:31.2
==> 06:01:31.299
请注意,中的S
模式java.time.DateTimeFormatter
已从更改millisecond
为fraction-of-second
就个人而言,最清晰的选择是使用java.time.Instant
:
import java.time.Instant
import java.time.ZoneId
import java.time.format.DateTimeFormatter
println Instant.now()
.atZone(ZoneId.of("CET"))
.format(DateTimeFormatter.ofPattern("HH:mm:ss.SSS"))
==> 06:12:22.916
推荐阅读
- ibm-datapower - IBM APIC 2018.1 与 DataPower 7.5.2.0 连接
- angular - 无法将子组件导入父组件
- docker-compose - 在 docker-compose 中发出运行命令
- python - 是否可以将kombu用作activeMQ的客户端库?
- java - 当你可以做一个接口时,抽象类有什么意义
- postgresql - 使用过滤器在数据库中查找表的子元素(DBeaver)
- wso2 - 注册 iOS 设备时发生密钥库错误
- apache-spark - Scala、IntelliJ 和 Gradle 依赖项
- formula - 总结所有以前的,直到某一点
- c# - 使用反射c#映射两个对象