首页 > 解决方案 > 升级到 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 行

我究竟做错了什么?

标签: groovysoapui

解决方案


您应该可以访问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

使用ZonedDateTimefrom 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已从更改millisecondfraction-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

推荐阅读