Organizational Research By

Surprising Reserch Topic

is there a stopwatch in java using -'java,stopwatch'


is there a stopwatch in java  using -'java,stopwatch'

Is there a stopwatch in java because on google I only find codes of stopwatches and they don't work, they give me always 0 miliseconds.

This code that I found doesn't work but I don't see why because the code looks fine to me.

public class StopWatch {

  private long startTime = 0;
  private long stopTime = 0;
  private boolean running = false;


  public void start() {
    this.startTime = System.currentTimeMillis();
    this.running = true;
  }


  public void stop() {
    this.stopTime = System.currentTimeMillis();
    this.running = false;
  }


  //elaspsed time in milliseconds
  public long getElapsedTime() {
    long elapsed;
    if (running) {
      elapsed = (System.currentTimeMillis() - startTime);
    }
    else {
      elapsed = (stopTime - startTime);
    }
    return elapsed;
  }


  //elaspsed time in seconds
  public long getElapsedTimeSecs() {
    long elapsed;
    if (running) {
      elapsed = ((System.currentTimeMillis() - startTime) / 1000);
    }
    else {
      elapsed = ((stopTime - startTime) / 1000);
    }
    return elapsed;
  }
}

    
asked Sep 7, 2015 by rajesh
0 votes
2 views



Related Hot Questions



Government Jobs Opening


...