6 replies
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.
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
by rts0
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.
Trending Web Development posts
Sort by Trending