0

I am trying to calculate difference between times required to perform specific tasks. For example,

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

By doing above I am getting proper out as 5 mins, 23 secs etc. Similarly

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

By doing above I am getting proper out as 2 mins, 23 secs etc.

But for some requirement i need to calculate difference between both the task I tried doing

TimeDuration diffbetween2task = TimeCategory.minus( task1, task2 )

but it gives an error. It expects task1 and task2 as date format. Can someone please help?

user_9090
  • 1,884
  • 11
  • 28
  • TimeCategory.minus has two different implementations: static Date minus​(Date date, BaseDuration duration) static TimeDuration minus​(Date lhs, Date rhs), you are using TimeDuration one, take a look at the docs: http://docs.groovy-lang.org/latest/html/api/groovy/time/TimeCategory.html#minus(java.util.Date,groovy.time.BaseDuration) – User_67128 Oct 14 '19 at 15:31
  • Possible duplicate of [How to find the duration of difference between two dates in java?](https://stackoverflow.com/questions/17940200/how-to-find-the-duration-of-difference-between-two-dates-in-java) – ivanjermakov Oct 14 '19 at 15:36

1 Answers1

0

use just task1 - task2

that's because the class TimeDuration has method 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

output:

time taken by task1 is 1.235 seconds
time taken by task2 is 2.345 seconds
diff 1.110 seconds
daggett
  • 26,404
  • 3
  • 40
  • 56