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

StringformatTimeInterval(final long time)
Formats a time interval and returns it as a String .
long milliseconds = time % 1000;
long seconds = (time / 1000) % 60;
long minutes = (time / (1000 * 60)) % 60;
long hours = (time / (1000 * 60 * 60)) % 24;
return String.format("%s:%s:%s.%s", (((hours < 10) ? "0" : "") + String.valueOf(hours)),
        (((minutes < 10) ? "0" : "") + String.valueOf(minutes)),
        (((seconds < 10) ? "0" : "") + String.valueOf(seconds)),
        (((milliseconds < 10) ? "00" : ((milliseconds < 100) ? "0" : "")) + String.valueOf(milliseconds)));
...
StringformatTimeInterval(long in)
return a string a time interval in millisec
int del = (int) (in + 500) / 1000; 
int seconds = del % 60;
del /= 60;
int minute = del % 60;
del /= 60;
int hour = del % 24;
del /= 24;
int days = del;
...
StringformatTimeLength(long ms)
format Time Length
int SECOND = 1000;
int MINUTE = SECOND * 60;
int HOUR = MINUTE * 60;
int DAY = HOUR * 24;
if (ms < SECOND)
    return ms + " ms";
if (ms < MINUTE)
    return round((ms / (float) SECOND), 2) + " secs";
...
StringformatTimeLikeTimer(long time, boolean appendMs)
Formats an long integer time value in format (-)hh:mm:ss(.S)
boolean isPositive = (time >= 0);
if (!isPositive) {
    time *= -1;
long milliSeconds = time;
long seconds = time / 1000;
long minutes = seconds / 60;
long hours = minutes / 60;
...
StringformatTimeNicely(long millis)
format Time Nicely
int seconds = (int) Math.round(millis / 1000.0);
int minutes = seconds / 60;
int hours = minutes / 60;
seconds = seconds % 60;
minutes = minutes % 60;
if (hours > 0) {
    return hours + ":" + String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
} else {
...
StringformatTimeOffset(long offset)
format Time Offset
if (offset == 0)
    return "no time";
long tmp = offset;
StringBuffer ret = new StringBuffer();
long sec = tmp % 60;
timeOffsetPrepend(sec, "second", "", ret);
tmp = tmp / 60;
long min = tmp % 60;
...
StringformatTimePart2(long number, String unit)
format Time Part
String result = number + " " + unit;
if (number > 1) {
    result += "s";
return result;
StringformatTimePeriod(long millis)
format Time Period
long seconds = (millis / 1000) % 60;
long minutes = (millis / (1000 * 60)) % 60;
long hours = (millis / (1000 * 60 * 60));
long onlyMillis = millis % 1000;
boolean hasMillis = onlyMillis > 0;
if (hours > 0) {
    String format = "%d:%02d:%02d";
    if (hasMillis) {
...
StringformatTimePeriod(long timePeriod)
Formats a time period in legible terms - not a date represented as a long, but a period of time (eg processing time)
int hours = (int) timePeriod / 3600000;
timePeriod = timePeriod % 3600000;
int minutes = (int) timePeriod / 60000;
timePeriod = timePeriod % 60000;
int seconds = (int) timePeriod / 1000;
timePeriod = timePeriod % 1000;
return (hours > 0 ? hours + "h " : "") + (minutes > 0 ? minutes + "m " : "")
        + (seconds > 0 ? seconds + "s " : "") + (timePeriod > 0 ? timePeriod + "ms " : "");
...
StringformatTimePeriod(long timestamp)
format Time Period
if (timestamp < 1000)
    return timestamp + " ms";
if (timestamp < 60 * 1000)
    return (timestamp / 1000) + " sec";
if (timestamp < 60 * 60 * 1000)
    return (timestamp / (1000 * 60)) + " min";
if (timestamp < 24 * 60 * 60 * 1000)
    return (timestamp / (1000 * 60 * 60)) + " h";
...