Skip to content

Instantly share code, notes, and snippets.

@vahidhedayati
Created October 21, 2016 10:01
Show Gist options
  • Save vahidhedayati/d654fdb7f0f8be2bcae4489fd03687de to your computer and use it in GitHub Desktop.
Save vahidhedayati/d654fdb7f0f8be2bcae4489fd03687de to your computer and use it in GitHub Desktop.
TimeDuration removing milliseconds.md

When you have TimeCategory do calculation and return duration difference you will get it took 1.001 seconds for example.. To get rid of the milliseconds I used this hack, take from Tim Yates here:

http://stackoverflow.com/questions/16716802/how-to-format-time-durations-in-groovy

A slight tweak to set millseconds to 0 then parse out .000 from output...

  private String getDuration(Date startDate,Date endDate) {
		if (startDate && endDate) {
			//return TimeCategory.minus(endDate, startDate)
			TimeDuration customPeriod = use(TimeCategory ) {
				customDuration( endDate - startDate )
			}
			return customPeriod.toString().replace('.000','')
		}
	}
	
	TimeDuration customDuration( TimeDuration tc ) {
		// Replacing tc.mills with 0 then parsing it out above
		new TimeDuration(  tc.days , tc.hours, tc.minutes, tc.seconds, 0)
	}

Tim is a legend :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment