Java Utililty Methods Time Format

List of utility methods to do Time Format

Description

The list of methods to do Time Format are organized into topic(s).

Method

StringformatTime(Integer seconds)
format Time
Integer hours = seconds / 3600;
Integer remainder = seconds % 3600;
Integer mins = remainder / 60;
Integer secs = remainder % 60;
if (hours > 0) {
    return hours + ":" + mins + ":" + secs + " hours";
} else if (mins > 0) {
    if (secs < 10) {
...
StringformatTime(long dtNanos)
format Time
String x = "" + dtNanos;
while (x.length() < 7) {
    x = "0" + x;
return x.substring(0, x.length() - 6) + "." + x.substring(x.length() - 6);
StringformatTime(long elapsedTime)
format Time
String format = String.format("%%0%dd", 2);
elapsedTime = elapsedTime / 1000;
String seconds = String.format(format, elapsedTime % 60);
String minutes = String.format(format, (elapsedTime % 3600) / 60);
String hours = String.format(format, elapsedTime / 3600);
String time = hours + ":" + minutes + ":" + seconds;
return time;
StringformatTime(long elapsedTime)
Formats a long value denoting a time interval into a more readable String .
final long millisToPrint = ((elapsedTime % ONE_HOUR) % ONE_MIN) % ONE_SEC;
final long secondsToPrint = ((elapsedTime % ONE_HOUR) % ONE_MIN) / ONE_SEC;
final long minutesToPrint = (elapsedTime % ONE_HOUR) / ONE_MIN;
final long hoursToPrint = elapsedTime / ONE_HOUR;
String result = null;
if (hoursToPrint > 0) {
    result = hoursToPrint + " hr";
if (minutesToPrint > 0) {
    result = (result == null ? "" : (result + " ")) + minutesToPrint + " min";
if (secondsToPrint > 0) {
    result = (result == null ? "" : (result + " ")) + secondsToPrint + " sec";
result = (result == null ? "" : (result + " ")) + millisToPrint + " msec";
return result;
StringformatTime(long millis)
format Time
long secs = millis / 1000;
long s = secs % 60;
long m = (secs / 60) % 60;
long h = secs / 3600 % 24;
long d = secs / (3600 * 24);
StringBuilder sb = new StringBuilder();
if (d > 0)
    sb.append(d + "d ");
...
StringformatTime(long millis)
format Time
long seconds = millis / 1000;
if (seconds < 0) {
    return millis + "ms";
} else if (seconds < 60) {
    return seconds + "s";
} else {
    long minutes = seconds / 60;
    seconds = seconds % 60;
...
StringformatTime(long millis)
format Time
StringBuffer sb = new StringBuffer();
long n = millis / HOURS;
if (n != 0) {
    sb.append(n).append("h");
n = (millis % HOURS) / MINUTES;
if ((n) != 0) {
    sb.append(n).append("m");
...
StringformatTime(long millis)
format Time
if (millis == Long.MAX_VALUE) {
    return "LIVE";
long t = millis / 1000L;
int sec = (int) (t % 60L);
int min = (int) ((t % 3600L) / 60L);
int hrs = (int) (t / 3600L);
String timestamp;
...
StringformatTime(long millis, int precision)
format Time
long[] la = new long[5];
la[0] = (millis / 86400000);
la[1] = (millis / 3600000) % 24;
la[2] = (millis / 60000) % 60;
la[3] = (millis / 1000) % 60;
la[4] = (millis % 1000);
int index = 0;
for (int i = 0; i < la.length; i++) {
...
StringformatTime(long ms)
format Time
long val = ms / MS_PER_SECOND;
final int seconds = (int) (val % SECONDS_PER_MIN);
val /= SECONDS_PER_MIN;
final int minutes = (int) (val % MIN_PER_HOUR);
val /= MIN_PER_HOUR;
final int hours = (int) (val % HOURS_PER_DAY);
val /= HOURS_PER_DAY;
final int days = (int) val;
...