您可能可以使用此类来计算经过的时间。
public class StopWatch {
/* Private Instance Variables */
/** Stores the start time when an object of the StopWatch class is initialized. */
private long startTime;
/**
* Custom constructor which initializes the {@link #startTime} parameter.
*/
public StopWatch() {
startTime = System.currentTimeMillis();
}
/**
* Gets the elapsed time (in seconds) since the time the object of StopWatch was initialized.
*
* @return Elapsed time in seconds.
*/
public double getElapsedTime() {
long endTime = System.currentTimeMillis();
return (double) (endTime - startTime) / (1000);
}
}
并像这样使用它:
public class SWTest {
public static void main(String[] args) {
StopWatch stopWatch = new StopWatch();
doSomething();
LOGGER.debug("Execution took in seconds: ", (stopWatch.getElapsedTime());
}
}