Skip to content

Instantly share code, notes, and snippets.

@CarlosPanarello
Created March 4, 2021 19:20
Show Gist options
  • Save CarlosPanarello/ad784ccb96958b5f443d5906c4e80a6f to your computer and use it in GitHub Desktop.
Save CarlosPanarello/ad784ccb96958b5f443d5906c4e80a6f to your computer and use it in GitHub Desktop.
Calculate the elapsed time in seconds
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.Instant;
public class CalcUtil {
private static final BigDecimal MULTIPLIER_NANO_TO_SECONDS = new BigDecimal(1.0E9D);
/**
* Calculate the elapsed time in seconds
*
* @param init initial time
* @return time in seconds
*/
public static double calcTimeElapsedInSeconds(Instant init) {
var finish = Instant.now();
BigDecimal diff = new BigDecimal(Duration.between(init, finish).toNanos());
return diff.divide(MULTIPLIER_NANO_TO_SECONDS, 9, RoundingMode.HALF_UP).doubleValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment