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

StringmillisecondsToSecondsString(long millis)
milliseconds To Seconds String
return millis / 1000 + "." + String.format("%03d", millis % 1000);
StringmilliSecondsToString(long interval)
Convert interval into formatted
int r = 0;
int hour = (int) interval / (60 * 60 * 1000);
r = (int) interval % (60 * 60 * 1000);
int minute = r / (60 * 1000);
r = r % (60 * 1000);
int second = r / (1000);
return hour + "h " + minute + "m " + second + "s";
StringmillisecondsToString(long milliseconds)
Converts the given milliseconds to string.
int seconds = (int) (milliseconds / 1000) % 60;
int minutes = (int) ((milliseconds / (1000 * 60)) % 60);
int hours = (int) ((milliseconds / (1000 * 60 * 60)) % 24);
return hours + " h " + minutes + " min " + seconds + " s";
StringmillisecondsToString(long time)
milliseconds To String
int milliseconds = (int) (time % 1000);
int seconds = (int) ((time / 1000) % 60);
int minutes = (int) ((time / 60000) % 60);
int hours = (int) ((time / 3600000) % 24);
String millisecondsStr = (milliseconds < 10 ? "00" : (milliseconds < 100 ? "0" : "")) + milliseconds;
String secondsStr = (seconds < 10 ? "0" : "") + seconds;
String minutesStr = (minutes < 10 ? "0" : "") + minutes;
String hoursStr = (hours < 10 ? "0" : "") + hours;
...
StringmillisecondsToTimerString(long millis)
milliseconds To Timer String
Date date = new Date(millis);
DateFormat formatter = new SimpleDateFormat("mm:ss");
return formatter.format(date);
StringmillisecondsToTimestamp(final long milliseconds)
Converts milliseconds to a timestamp string.
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SSS");
sdf.setTimeZone(new SimpleTimeZone(0, "NO_ZONE"));
return sdf.format(milliseconds);
StringmillisecondsToTimeString(int milliseconds)
milliseconds To Time String
double secondsDouble = (milliseconds / 1000d);
int secondsIn = (int) ((secondsDouble - (int) secondsDouble) > 0 ? secondsDouble + 1 : secondsDouble);
int hours = secondsIn / 3600;
int remainder = secondsIn % 3600;
int minutes = remainder / 60;
int seconds = remainder % 60;
return (hours > 0 ? hours + ":" : "") + (hours > 0 && minutes < 10 ? "0" : "") + minutes + ":"
        + (seconds < 10 ? "0" : "") + seconds;
...
StringmillisecondToDate(long timeMillis)
millisecond To Date
if (timeMillis <= 0)
    return "";
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeMillis);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(calendar.getTime());
StringmillisecondToDate(String str)
millisecond To Date
long ms = Long.parseLong(str);
Date dat = new Date(ms * 1000);
GregorianCalendar gc = new GregorianCalendar();
gc.setTime(dat);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String sb = format.format(gc.getTime());
return sb;
StringmillisecondToDHMS(long duration)
millisecond To DHMS
String res = "";
long temp = 0;
if (duration >= ONE_SECOND) {
    temp = duration / ONE_DAY;
    if (temp > 0) {
        res = temp + " day";
        if (temp > 1) {
            res += "s";
...