思想
定义两个常量:start、now
start为开始时间
now为结束时间
分别获取以毫秒计数的当前时间
实现
public class Stopwatch {
private final long start;
/** 创建一个计时器 **/
public Stopwatch() {
start = System.currentTimeMillis();
}
/** 返回对象创建以来所经过的时间 **/
public double elapsedTime() {
long now = System.currentTimeMillis();
// 将毫秒转换为秒并返回
return (now - start) / 1000.0;
}
}