1

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();
    }
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
Adam Grunt
  • 207
  • 1
  • 3
  • 16
  • I recommend you don’t use `SimpleDateFormat` and `Date`. Those classes are poorly designed and long outdated (the former in particular notoriously troublesome). Instead use `LocalDateTime`, `DateTimeFormatter` and `Duration`, all from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Apr 14 '19 at 02:39
  • From your strings I expected 24 h 1 min 11 secs. I got `0 hours, 1 minutes, 11 seconds.` If I comment in the two commented-out lines, I get `1 days, 0 hours, 1 minutes, 11 seconds.` Your way is very manual, which you don’t want, but what makes you think the method is wrong? – Ole V.V. Apr 14 '19 at 02:48
  • You should not store the start and end times as strings. Doing that is the sure way to make your task more complicated than it needs to be, If you need the date and time the script started and ended, use for example `Instant` or `ZonedDateTime`. You can always format them for presentation when you need that. – Ole V.V. Apr 14 '19 at 03:00
  • Only issue that I'm having is that it is returning time as in format 0:3:5 whereas I want them in like following format 00:03:05. Also storing them into String was just for example. – Adam Grunt Apr 14 '19 at 05:02
  • Good to read. The linked original question will solve the `0:3:5` for you. – Ole V.V. Apr 14 '19 at 05:04

1 Answers1

0

Use below code:-

TimeUnit Enum

The following expression uses the TimeUnit enum (Java 5 and later) to convert from nanoseconds to seconds:

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"); 
}}
Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37