Discussion Locked

Timestamp with the StopWatch class

0 helpful
6 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
Debugging code is often tedious. Will the use of the StopWatch class help you?

6 replies

by Mark W. Kaelin

I received this e-mail this morning and wanted to share it with everyone:

There is an error on your class in the getTimeString() method. Notice the
"millis" variable should be "milliseconds" just to let you know. Of course,
any real Java programmer would of caught the error and fixed it their self.
I just wanted to report it.

public String getTimeString() {
int hours, minutes, seconds, milliseconds;
long time = getTime();
hours = (int) (time / AN_HOUR); time = time - (hours * AN_HOUR);
minutes = (int) (time / A_MINUTE);
time = time - (minutes * A_MINUTE);
seconds = (int) (time / 1000);
time = time - (seconds * 1000);
millis = (int) time;return hours + "h:" + minutes + "m:" + seconds + "s:" + millis +
"ms";
}

Should be:

public String getTimeString() {
int hours, minutes, seconds, milliseconds;
long time = getTime();
hours = (int) (time / AN_HOUR);
time = time - (hours * AN_HOUR);
minutes = (int) (time / A_MINUTE);
time = time - (minutes * A_MINUTE);
seconds = (int) (time / 1000);
time = time - (seconds * 1000);
milliseconds= (int) time;

return hours + "h:" + minutes + "m:" + seconds + "s:" +
milliseconds+ "ms";
}

by richard l.

This will come in handy. Granted, many experienced programmers could or maybe already have written a similar routine. But I find that whenever I need a tool like this I'm too bogged down in the current morass to take the time to write the tool I need.

by Mark W. Kaelin

I received this in an e-mail - I thought it was interesting:

I really like that tip but I think that it could be enhanced by using
the SimpleDateFormat class. By doing so, you can avoid all of the
calculations you are having to perform in the getTimeString() method.

For example:
public String getTimeString() {

int hours, minutes, seconds, milliseconds;

long time = getTime();
hours = (int) (time / AN_HOUR);
time = time - (hours * AN_HOUR);
minutes = (int) (time / A_MINUTE);
time = time - (minutes * A_MINUTE);
seconds = (int) (time / 1000);
time = time - (seconds * 1000);
millis = (int) time;

return hours + "h:" + minutes + "m:" + seconds + "s:" + millis +
"ms";
}

Can berewritten as:

import java.util.Date;
import java.text.SimpleDateFormat;
:
:
public String getTimeString() {

long time = getTime();

SimpleDateFormat dateFormatter = new
SimpleDateFormat("h'h':mm'm':ss's':S'ms'");
return dateFormatter.format(new Date(time));
}

No big deal since its just a utility class but it avoids all that
calculating the original example was having to perform.

by solrak29

Its nice to see that you can skin a cat more than one way. To see the DateFormater and the actual date calculations with the data returned from the time call make for a nice quicky tutorial. The above metioned bug was really obvious, but such a class is always usefull expecialy when dealing with real-time applications.

by rts0

I executed your new getTime() and I keep getting '6h' for any given milliseconds assigned to time. The problem doesn't occur if I feed it the given milliseconds since epoch.

by Mark W. Kaelin

I have just been informed that there is an error in the code for this TechMail.

Currently the section reads as:
* Get the time on the stopwatch. This is either the
* time between start and latest split, between start and stop,
* or the time between the start and the moment this method is called.
*/
public long getTime() {
if(stopTime != -1) {
return (System.currentTimeMillis() - this.startTime);
} else {
return this.stopTime - this.startTime;
}
}

public String toString() {
return getTimeString();
}

It should read:
* Get the time on the stopwatch. This is either the
* time between start and latest split, between start and stop,
* or the time between the start and the moment this method is called.
*/
public long getTime() {
if(stopTime !== -1) {
return (System.currentTimeMillis() - this.startTime);
} else { return this.stopTime - this.startTime;
}
}

public String toString() {
return getTimeString();
}

Sorry if that slip caused any problems.

This discussion is closed to new replies.