我已经写了一个Selenium脚本,我想计算该脚本的总执行时间。如何减去脚本开始执行和结束时系统返回的时间?
我正在使用Java的Date
和SimpleDateFormat
类来获取系统时间,然后对其进行格式化,但是我要返回总时间似乎是错误的。
String dateStart = "01/14/2012 23:05:49";
String dateStop = "01/15/2012 23:07:00";
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
Date d1 = null;
Date d2 = null;
try {
d1 = sdf.parse(dateStart);
d2 = sdf.parse(dateStop);
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
//long diffDays = diff / (24 * 60 * 60 * 1000);
//System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:0)
使用以下代码:-
TimeUnit枚举
以下表达式使用TimeUnit枚举(Java 5和更高版本)将纳秒转换为秒:
public static void main (String[] args) {
long start = System.nanoTime();
//some try with nested loops
long end = System.nanoTime();
long elapsedTime = end - start;
System.out.println("elapsed: " + elapsedTime + "nano seconds\n");
//convert to seconds
TimeUnit seconds = new TimeUnit();
System.out.println("which is " + TimeUnit.NANOSECONDS.toSeconds(elapsedTime) + " seconds");
}}