/**
* 计算程序运行时间,展示形式为 时分秒
* */
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 block
e.printStackTrace();
}
long endTime = System.currentTimeMillis();
System.out.println(computationDiffTime(startTime, endTime));
}
08-01
5313
03-30
371
03-01
3544