Created
March 4, 2021 19:20
-
-
Save CarlosPanarello/ad784ccb96958b5f443d5906c4e80a6f to your computer and use it in GitHub Desktop.
Calculate the elapsed time in seconds
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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