Question: I came across a scenario where I need to capture time(seconds also) when I click on any link/button. Right now I am capturing, before doing that action. But some times(10%), its going to next second when code for clicking on link/button gets executed.

Please find code below:

```
DateFormat dateFormat1 = new SimpleDateFormat("yyyyMMdd_HHmmss");
String dateInFileName=dateFormat1.format(reportGeneratedTimeStamp).toString();

 // here its capturing as "20160923_091256"
// code to click on button -- But here time will be "20160923_091257"

```

So I want to get the time when exactly the action is done and dont want to take that 10% chances also :) Can any one please help me in fixing this issue?

Answer: 
(1) Because Seleninum WebDriver try to simulate human action, **cause delay time**.

(2) Your expected time has been add more 1 second with **probability** 10%, 90% it's work correctly.

(1), (2) => Has no solution, or you get time from Java program:

```
DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
Date date = new Date();
System.out.println(dateFormat.format(date)); //20160923_091256

```

It isn't automation test yet.
Most accurate way to test time in java is:

```
Long start = System.currentTimeMillis();

 // do some action //

Long end = System.currentTimeMillis();

```

and to get actual duration is with this method:

```
public String getDuration(long millis) {
    // long days = TimeUnit.MILLISECONDS.toDays(millis);
    // millis -= TimeUnit.DAYS.toMillis(days);

    millis = Math.abs(millis);

    long hours = TimeUnit.MILLISECONDS.toHours(millis);
    millis -= TimeUnit.HOURS.toMillis(hours);

    long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
    millis -= TimeUnit.MINUTES.toMillis(minutes);

    long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
    millis -= TimeUnit.SECONDS.toMillis(seconds);

    StringBuilder sb = new StringBuilder(64);
    sb.append(String.format("%02d", hours));
    sb.append(":");
    sb.append(String.format("%02d", minutes));
    sb.append(":");
    sb.append(String.format("%02d", seconds));
    sb.append(".");
    sb.append(String.format("%02d", millis));
    return (sb.toString());
}

```

and to get some duration as string

```
System.out.println(getDuration(end-start));

```

And this is actualy 100% accurate because its system time and its handled in milliseconds.