gpt4 book ai didi

java - 如何计算格式为 TimeDuration 的时间差

转载 作者:行者123 更新时间:2023-12-01 23:10:25 26 4
gpt4 key购买 nike

我正在尝试计算执行特定任务所需的时间之间的差异。例如,

Date startpipeline = new Date()
// ...task 1...
Date stoppipeline = new Date()
TimeDuration task1 = TimeCategory.minus( stoppipeline, startpipeline )
print "time taken by task1 is " + task1

通过执行上述操作,我得到的结果为 5 分钟 23 秒 等。同样

Date startpipeline2 = new Date()
// ...task2...
Date stoppipeline2 = new Date()
TimeDuration task2 = TimeCategory.minus( stoppipeline2, startpipeline2 )
print "time taken by task2 is " + task2

通过执行上述操作,我得到的结果为 2 分 23 秒 等。

但是对于某些要求,我需要计算我尝试执行的任务之间的差异

TimeDuration diffbetween2task = TimeCategory.minus( task1, task2 )

但它给出了一个错误。它期望任务 1 和任务 2 为日期格式。有人可以帮忙吗?

最佳答案

仅使用任务1 - 任务2

这是因为 TimeDuration 类有方法 minus .

import groovy.time.*

Date startpipeline = new Date()
Thread.sleep(1234)
Date stoppipeline = new Date()
TimeDuration task1 = TimeCategory.minus( stoppipeline, startpipeline )
println "time taken by task1 is " +task1


startpipeline = new Date()
Thread.sleep(2345)
stoppipeline = new Date()
TimeDuration task2 = TimeCategory.minus( stoppipeline, startpipeline )
println "time taken by task2 is " +task2


TimeDuration diff = task2-task1
println "diff " +diff

输出:

time taken by task1 is 1.235 seconds
time taken by task2 is 2.345 seconds
diff 1.110 seconds

关于java - 如何计算格式为 TimeDuration 的时间差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58379481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com