Skip to content

Instantly share code, notes, and snippets.

@sontqq
Created October 30, 2019 23:10
Show Gist options
  • Save sontqq/cb4bb2ad415c78b6c2302fe0a49a5396 to your computer and use it in GitHub Desktop.
Save sontqq/cb4bb2ad415c78b6c2302fe0a49a5396 to your computer and use it in GitHub Desktop.
TimeElapsedUtil.java
package sample;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class TimeElapsedUtil {
public long startTime;
public long now;
public void setStartTime(long startTime) {
this.startTime = startTime;
}
public TimeElapsedUtil() {
this.startTime = System.currentTimeMillis();
this.now = System.currentTimeMillis();
}
public TimeElapsedUtil(long startTime) {
this.startTime = startTime;
this.now = System.currentTimeMillis();
}
public TimeElapsedUtil(long startTime, long now) {
this.startTime = startTime;
this.now = now;
}
public String getElapsed() {
this.now = System.currentTimeMillis();
return convertLongToHRString(this.now - this.startTime);
}
private String convertLongToHRString(long val) {
Date date = new Date(val);
DateFormat formatter = new SimpleDateFormat("HH:mm:ss.SSS");
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
return formatter.format(date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment