Sunday 1 September 2019

selenium - How to subtract two system times using Java

I have written a Selenium script and I want to calculate the total execution time of the script. How do I subtract the time returned by system when the script started executing and when it’s ended?



I'm using Date and SimpleDateFormat class of Java to get the system time and then format it but the method seems wrong that I'm following to return total time.




    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();
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...