Posted By

laurenceosx on 03/31/11


Tagged

groovygroovytime


Versions (?)

Groovy Time Difference Snippets


 / Published in: Groovy
 

  1. import org.codehaus.groovy.runtime.TimeCategory;
  2.  
  3. def timeDiffMinutes( aStart, aStop ) {
  4. def result = ( ( aStop.time - aStart.time ) / 1000.0 / 60.0 ) as double;
  5. return result;
  6. }
  7.  
  8. def timeDiffMinutesAbs( aDate1, aDate2 ) {
  9. return Math.abs( timeDiffMinutes( aDate1, aDate2 ) ) as double;
  10. }
  11.  
  12. def timeDiff( aDate1, aDate2 ) {
  13. def result;
  14. use(TimeCategory) {
  15. result = aDate2 - aDate1;
  16. }
  17. return result;
  18. }
  19.  
  20. def timeDiffAbs( aDate1, aDate2 ) {
  21. def args = [aDate1, aDate2].sort()
  22. return timeDiff( args[0], args[1] )
  23. }
  24.  
  25. def timeIncMin( aDate, aMin, aSec = 0 ) {
  26. def result;
  27. use(TimeCategory) {
  28. result = aDate + aMin.minutes + aSec.seconds;
  29. }
  30. return result;
  31. }
  32.  
  33. def timeDecMin( aDate, aMin, aSec = 0 ) {
  34. def result = timeIncMin( aDate, -aMin, -aSec );
  35. return result;
  36. }
  37.  
  38. println "-" * 30
  39.  
  40. t1 = new Date()
  41. min = 0 as int
  42. sec = 60 as int
  43. t2 = timeDecMin( t1, min, sec )
  44.  
  45. def t1 = new Date();
  46. def t2 = timeIncMin( t2, min, sec );
  47.  
  48. println '---'
  49.  
  50. t1 = new Date()
  51. t2 = timeIncMin( t1, 30 )
  52.  
  53. println "t1: $t1 t2: $t2"
  54. def delta = timeDiff( t1, t2 );
  55. println delta
  56.  
  57.  
  58. println "t1: $t1 t2: $t2"
  59. delta = timeDiff( t2, t1 );
  60. println delta
  61. delta = timeDiffAbs( t2, t1 );
  62. println delta
  63.  
  64. println timeDiffMinutesAbs( t2, t1 ) ;
  65.  
  66. groovy.inspect.swingui.ObjectBrowser.inspect( delta );

Report this snippet  

You need to login to post a comment.