计算程序运行时间,并将毫秒换算成人看得懂的文字,展示形式为时分秒
生活随笔
收集整理的這篇文章主要介紹了
计算程序运行时间,并将毫秒换算成人看得懂的文字,展示形式为时分秒
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 計算程序運行時間,展示形式為 時分秒* */public static String computationDiffTime(long startTime, long endTime) {// 差值long diffValue = endTime - startTime;int hour = 1000 * 60 * 60;// 定義小時的計算常量int minute = 1000 * 60;//定義分鐘的計算常量int second = 1000;//定義秒的計算常量String resultSize = "";// 格式化小數DecimalFormat df = new DecimalFormat("0.00");if (diffValue / hour >= 1) {resultSize = df.format(diffValue / (float) hour) + "時";} else if (diffValue / minute >= 1) {resultSize = df.format(diffValue / (float) minute) + "分";} else if (diffValue / second >= 1) {resultSize = df.format(diffValue / (float) second) + "秒";} else {resultSize = diffValue + "毫秒";}return resultSize;}/*** 測試* */public static void main(String[] args) {long startTime = System.currentTimeMillis();// 模擬睡眠六秒try {Thread.sleep(6000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}long endTime = System.currentTimeMillis();System.out.println(computationDiffTime(startTime, endTime));}
總結
以上是生活随笔為你收集整理的计算程序运行时间,并将毫秒换算成人看得懂的文字,展示形式为时分秒的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: servlet报错:org.apache
- 下一篇: 曙光计算机能玩游戏吗,《曙光》PC性能分