Adding run times in Goovy using java.sql.Time class
I'm trying to figure out a way add run times of interviews in the s开发者_如何学Goystem I'm working on in Groovy.
In my code I have an ArrayList of java.sql.Time objects ("times" in the below code) I would like to sum the total time of the interviews in the ArrayList with a closure. But I cannot find a valid way to add together to java.sql.Times. So, if my ArrayList contains the sql times "00:01:30" and "00:02:30" the method would return "00:04:00".
def sumTime(java.util.ArrayList times) {
def sum = java.sql.Time.valueOf("00:00:00")
times.each() { x ->
//add current list member to sum
}
return sum
}
Thanks,
Don
You can do this (edit this works in both the webconsole and the desktop console):
import java.sql.Time
def timeList = [
Time.valueOf( '00:01:30' ),
Time.valueOf( '00:02:30' ),
]
def sumTime( times ) {
long zero = Time.valueOf( '00:00:00' ).time
new Time( zero + times.sum { it.time - zero } )
}
println sumTime( timeList )
which prints
00:04:00
as required
The original issue with this code is that on the Google App Engine JDK, new Time( '00:00:00' ).time
returns 0
, wheras on the ex-Sun 1.6 JVM, the same code returns -3600000
精彩评论