Datetime 使用Groovy将时间四舍五入到最接近的15分钟时间

Datetime 使用Groovy将时间四舍五入到最接近的15分钟时间,datetime,groovy,soapui,rounding,Datetime,Groovy,Soapui,Rounding,我试图把时间缩短到接下来的15分钟。 e、 g: 2017-12-11T13:11:51.728Zto2017-12-11T13:15:00.000Z 2017-12-11T13:21:51.728Zto2017-12-11T13:30:00.000Z Groovy代码: def currentTime = context.expand('${#Project#currentTime}') log.info currentTime date1 = new SimpleDateFormat("y

我试图把时间缩短到接下来的15分钟。 e、 g:

  • 2017-12-11T13:11:51.728Z
    to
    2017-12-11T13:15:00.000Z
  • 2017-12-11T13:21:51.728Z
    to
    2017-12-11T13:30:00.000Z
  • Groovy代码:

    def currentTime = context.expand('${#Project#currentTime}')
    log.info currentTime
    date1 =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(currentTime)
    //Round off the time nearest to the mod 15
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date1);
    int unroundedMinutes = calendar.get(Calendar.MINUTE);
    int mod = unroundedMinutes % 15;
    log.info calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod));
    
    def currentTime=context.expand(“${{Project}currentTime}”)
    log.info当前时间
    date1=新的SimpleDataFormat(“yyyy-MM-dd'T'HH:MM:ss.SSS'Z')。解析(currentTime)
    //将最接近mod 15的时间四舍五入
    日历=Calendar.getInstance();
    日历。设置时间(日期1);
    int unroundedMinutes=calendar.get(calendar.MINUTE);
    int mod=未取整分钟%15;
    log.info calendar.add(calendar.MINUTE,mod<8?-mod:(15 mod));
    
    输出:

    def currentTime = context.expand('${#Project#currentTime}')
    log.info currentTime
    date1 =  new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").parse(currentTime)
    //Round off the time nearest to the mod 15
    Calendar calendar = Calendar.getInstance();
    calendar.setTime(date1);
    int unroundedMinutes = calendar.get(Calendar.MINUTE);
    int mod = unroundedMinutes % 15;
    log.info calendar.add(Calendar.MINUTE, mod < 8 ? -mod : (15-mod));
    
    2017年12月11日星期一14:32:32 IST:信息:2017-12-11T14:32:32.690Z
    2017年12月11日星期一14:32:32 IST:信息:空

    给你:

    • 为了获得差异分钟数,创建了一个闭包
    • 如果需要,将递归调用闭包
    • 如果当前分钟数可除以15,则不会调整时间;这就是在列表中添加第三个值的原因
    • 为了能够使用多个值进行测试,使用了日期列表。您也可以将其用于单个值
    def timez=['2017-12-11T13:11:51.728Z','2017-12-11T13:21:51.728Z','2017-12-11T13:30:00.000Z']
    def dateFormat=“yyyy-MM-dd'T'HH:MM:ss.SSS'Z”
    def roundValue=15
    //如果需要,更改时区
    def tz='IST'
    TimeZone.setDefault(TimeZone.getTimeZone(tz))
    Calendar=Calendar.getInstance()
    def获得最接近的分钟数
    //被称为递归的闭包
    getNearestMinutes={cmin,nearMin=roundValue->
    def tempResult=cmin%nearMin
    如果(tempResult
    你可以在网上快速试用

    编辑:认为解决方案可以比应用上述困难条件更简单

    下面是另一个解决方案,将时间舍入到最近的15分钟。 然而,与第一个解决方案中的多个条件不同,易于阅读的代码。 这是一个使用
    Switch语句的简单方法

    def timez=['2017-12-11T13:11:51.728Z','2017-12-11T13:21:51.728Z','2017-12-11T13:30:00.000Z','2017-12-11T13:46:00.000Z']
    def dateFormat=“yyyy-MM-dd'T'HH:MM:ss.SSS'Z”
    def roundValue=15
    //如果需要,更改时区
    def tz='IST'
    TimeZone.setDefault(TimeZone.getTimeZone(tz))
    Calendar=Calendar.getInstance()
    def getNexTime={min->
    def结果
    开关(最小值){
    案例1..15:
    结果=15
    打破
    案例16..30:
    结果=30
    打破
    案例31..45:
    结果=45
    打破
    案例46..60:
    结果=60
    打破
    违约:
    结果=0
    打破
    }
    结果
    }
    //循环通过时间和循环时间
    蒂梅兹{
    calendar.time=Date.parse(dateFormat,it)
    def currentMinute=calendar.get(calendar.MINUTE)
    如果(0!=getNexTime(当前分钟)){
    calendar.set(calendar.MINUTE、getNexTime(currentMinute))
    }
    calendar.set(calendar.SECOND,0)
    calendar.set(calendar.毫秒,0)
    println calendar.time.format(dateFormat)
    }
    
    “currentTime”是字符串类型,将其转换为date解析为date后,现在输出为“null”。您从上下文中获得的是什么。展开(“${Project}currentTime}”)?它是当前日期时间:2017-12-11T13:11:51.728ZAre您使用Java 8吗?如果是这样的话,
    Date
    Calendar
    还有其他数据类型,谢谢@Rao,差不多完成了……我只需要这种格式的输出(2017-12-11T13:15:00.000Z)。秒和毫秒应为零。仅在几分钟后更新。请单击解决方案下方的“编辑”链接检查更改,或者再次尝试或尝试demo.perfer。谢谢:)@rAJ,谢谢你发现它很有用。您可以看到第二个解决方案,它的逻辑更简单。