Date startTime = new Date();
Thread.sleep(1000);//逻辑代码
Date endTime = new Date();
long lt = endTime.getTime() - startTime.getTime();
long day = lt / (24 * 60 * 60 * 1000);
long hour = (lt / (60 * 60 * 1000) - day * 24);
long min = ((lt / (60 * 1000)) - day * 24 * 60 - hour * 60);
long s = (lt / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
String msg = "消耗时间:"+hour + "时" + min + "分" + s + "秒(共" +lt +"毫秒)";
System.out.println(msg);
开发日志:计算Java内一段代码运行所用的时间,布布扣,bubuko.com
开发日志:计算Java内一段代码运行所用的时间