Java Utililty Methods Millisecond Convert

List of utility methods to do Millisecond Convert

Description

The list of methods to do Millisecond Convert are organized into topic(s).

Method

longmillisToSeconds(final long millis)
Converts millis to seconds
return Math.round(millis / 1000.0);
longmillisToSeconds(long millis)
Rounds the number of milliseconds relative to the epoch down to the nearest whole number of seconds.
if (millis >= 0) {
    return millis / 1000;
} else {
    return (millis - 999) / 1000;
StringmillisToSeconds(long millis)
Converts time in milliseconds to a String representing the time in seconds in the format XX.XXX
String result = "";
String complement = "";
long fraction = Math.abs(millis) % 1000;
if (fraction < 10) {
    complement = "00";
} else if (fraction < 100) {
    complement = "0";
result += millis / 1000;
result += ".";
result += complement + fraction;
return result;
longmillisToSeconds(long millis)
millis To Seconds
return millis / 1000;
StringmillisToShortDHMS(long duration)
converts time (in milliseconds) to human-readable format "hh:mm:ss"
String res = "";
duration /= ONE_SECOND;
int seconds = (int) (duration % SECONDS);
duration /= SECONDS;
int minutes = (int) (duration % MINUTES);
duration /= MINUTES;
int hours = (int) (duration % HOURS);
int days = (int) (duration / HOURS);
...
StringmillisToString(float millis)
millis To String
int s = (int) millis % 60;
int m = ((int) ((millis / 60) % 60));
return (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s;
StringmillisToString(long millis)
millis To String
int length = (int) Math.floor(millis / 1000D);
int hours = (int) Math.floor(length / 60D / 60D);
length -= hours * 60 * 60;
int minutes = (int) Math.floor(length / 60D);
length -= minutes * 60;
return (hours > 0 ? hours + ":" : "") + (minutes < 10 & hours > 0 ? "0" : "") + minutes + ":"
        + (length < 10 ? "0" + length : length);
StringmillisToString(long ms)
millis To String
int days = (int) (ms / (1000 * 60 * 60 * 24)) % 7;
int hours = (int) (ms / (1000 * 60 * 60)) % 24;
int mins = (int) (ms / (1000 * 60)) % 60;
int secs = (int) (ms / 1000) % 60;
if (days > 0) {
    return String.format("%s days, %sh %sm %ss", days, hours, mins, secs);
} else if (hours > 0) {
    return String.format("%sh %sm %ss", hours, mins, secs);
...
StringmillisToString(long t)
millis To String
StringBuilder sb = new StringBuilder();
long frac = t / 3600000L;
if (frac > 0) {
    sb.append(frac).append("h");
    t = t - frac * 3600000;
frac = t / 60000;
if (frac > 0) {
...
StringmillisToStringDouble(double millis)
millis To String Double
double seconds = millis / 1000d;
long hours = (long) (seconds / 3600);
long minutes = (long) ((seconds % 3600) / 60);
double remaining = (seconds % 3600d) % 60d;
String output = "";
output = output.concat(String.format("%02d:", hours));
output = output.concat(String.format("%02d:", minutes));
String stringifiedSeconds = decimalFormat.format(remaining);
...