升级到 SoapUI Pro 3.3.2 - 日期问题

Upgraded to SoapUI Pro 3.3.2 - Date problem

我已将 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) error at line: 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

使用来自 java.timeZonedDateTime:

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

请注意 java.time.DateTimeFormatter 中的 S 模式已从 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