【问题标题】:Upgraded to SoapUI Pro 3.3.2 - Date problem升级到 SoapUI Pro 3.3.2 - 日期问题
【发布时间】:2021-01-05 03:40:17
【问题描述】:

我将 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 行

我做错了什么?

【问题讨论】:

    标签: groovy soapui


    【解决方案1】:

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

    【讨论】:

    • 如何将 1 天添加到 SimpleDayFormatter 对象?
    • 还有一个问题,如何获取 ISODateTimeFormat?示例:2020-10-16T11:00:00.000+02:00
    • @user3624378 您应该将这些作为新问题提出。但是请注意,它们可能会作为重复项被关闭,因为它们已经被询问和回答。也就是说,如果您搜索它们,您应该会找到一些结果:)
    • 是的,offcourse - 我已经用 1 天和 isoformat 弄明白了。感谢您的回复:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多