Return to Snippet

Revision: 43800
at March 31, 2011 03:11 by laurenceosx


Initial Code
import org.codehaus.groovy.runtime.TimeCategory;

def timeDiffMinutes( aStart, aStop ) {
	def result = ( ( aStop.time - aStart.time ) / 1000.0 / 60.0 ) as double;
	return result;
}

def timeDiffMinutesAbs( aDate1, aDate2 ) {
	return Math.abs( timeDiffMinutes( aDate1, aDate2 ) ) as double;
}

def timeDiff( aDate1, aDate2 ) {
	def result;
	use(TimeCategory) {
    	result = aDate2 - aDate1;
	}
	return result;	
}

def timeDiffAbs( aDate1, aDate2 ) {
	def args = [aDate1, aDate2].sort()
	return timeDiff( args[0], args[1] )
}

def timeIncMin( aDate, aMin, aSec = 0 ) {
	def result;
	use(TimeCategory) {
    	result = aDate + aMin.minutes + aSec.seconds;
	}
	return result;
}

def timeDecMin( aDate, aMin, aSec = 0 ) {
	def result = timeIncMin( aDate, -aMin, -aSec );
	return result;
}

println "-" * 30
println new Date()
println ''

t1 = new Date()
min = 0 as int
sec = 60 as int
t2 = timeDecMin( t1, min, sec )
println t2

def t1 = new Date();
def t2 = timeIncMin( t2, min, sec );
println t1

println '---'

t1 = new Date()
t2 = timeIncMin( t1, 30 ) 

println "t1: $t1    t2: $t2"
def delta = timeDiff( t1, t2 );
println delta

println ""

println "t1: $t1    t2: $t2"
delta = timeDiff( t2, t1 );
println delta
delta = timeDiffAbs( t2, t1 );
println delta

println timeDiffMinutesAbs( t2, t1 ) ;

groovy.inspect.swingui.ObjectBrowser.inspect( delta );

Initial URL

                                

Initial Description

                                

Initial Title
Groovy Time Difference Snippets

Initial Tags

                                

Initial Language
Groovy