如何通过Java8的Instant去统计程序执行时间?


代码如下所示

import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;

public class DurationDemo {

    public static void main(String[] args) throws InterruptedException {
        //开始时间
        Instant stime = java.time.Instant.now();
        //程序执行过程
        TimeUnit.SECONDS.sleep(2);
        //结束时间
        Instant etime = java.time.Instant.now();
        //统计时间_毫秒
        System.out.println(Duration.between(stime, etime).toMillis());
        //统计时间 _秒
        System.out.println(Duration.between(stime, etime).getSeconds());
        //统计时间_分钟
        System.out.println(Duration.between(stime, etime).toMinutes());
    }
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM