方法一:
import java.time.Duration;
import java.time.LocalDateTime;
LocalDateTime beginTime = LocalDateTime.now();
Thread.sleep(1000);
Long opetime = Duration.between(beginTime, LocalDateTime.now()).toMillis();
System.out.println(opetime);
方法二:
Long beginTime = System.currentTimeMillis();
Thread.sleep(1000);
Long opetime = System.currentTimeMillis() - beginTime;
System.out.println(opetime);
方法三:
import com.google.common.base.Stopwatch;
Stopwatch stopwatchGroup = Stopwatch.createStarted();
// 业务代码
long groupExecuteTime = stopwatchGroup.stop().elapsed(TimeUnit.SECONDS);
LOGGER.info("耗时:{} 秒", groupExecuteTime);