Java Utililty Methods TimeUnit Usage

List of utility methods to do TimeUnit Usage

Description

The list of methods to do TimeUnit Usage are organized into topic(s).

Method

StringformatInterval(final long l)
Formatuje czas w milisekundach na czas w formacie HH:MM:SS:ss
final long hr = TimeUnit.MILLISECONDS.toHours(l);
final long min = TimeUnit.MILLISECONDS.toMinutes(l - TimeUnit.HOURS.toMillis(hr));
final long sec = TimeUnit.MILLISECONDS
        .toSeconds(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
final long ms = TimeUnit.MILLISECONDS.toMillis(
        l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min) - TimeUnit.SECONDS.toMillis(sec));
return String.format("%02d:%02d:%02d.%03d", hr, min, sec, ms);
StringformatMillis(final long millis)
format Millis
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis)
        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis)
        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis));
long hours = TimeUnit.MILLISECONDS.toHours(millis);
return (hours == 0 ? "00" : hours < 10 ? String.valueOf("0" + hours) : String.valueOf(hours)) + ":"
        + (minutes == 0 ? "00" : minutes < 10 ? String.valueOf("0" + minutes) : String.valueOf(minutes))
        + ":"
...
StringformatMillis(final long millis)
Returns a string representing the time in millis in the format h min sec ms.
long hours = TimeUnit.MILLISECONDS.toHours(millis);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) % 60;
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) % 60;
long _millis = millis % 1000;
return String.format("%dh %dmin %dsec %dms", hours, minutes, seconds, _millis);
StringformatMillis(long millis)
format Millis
return String.format("%dd:%dh:%dm:%ds:%dms", TimeUnit.MILLISECONDS.toDays(millis),
        TimeUnit.MILLISECONDS.toHours(millis) - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(millis)),
        TimeUnit.MILLISECONDS.toMinutes(millis)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
        TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)),
        millis - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(millis)));
StringformatMillis(long millis)
format Millis
long m = millis;
long hours = TimeUnit.MILLISECONDS.toHours(m);
m -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(m);
m -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(m);
m -= TimeUnit.SECONDS.toMillis(seconds);
return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, m);
...
StringformatMillisecs(long lMillisecs, final boolean bWantMSecs)
format Millisecs
if (lMillisecs <= 0L)
    return bWantMSecs ? "00:00.000" : "00:00";
final StringBuilder sb = new StringBuilder();
final int iHours = (int) TimeUnit.MILLISECONDS.toHours(lMillisecs);
if (iHours > 0) {
    lMillisecs -= TimeUnit.HOURS.toMillis(iHours);
    if (sb.length() > 0)
        sb.append(' ');
...
StringformatMillisIntoMinutesAndSeconds(long millis)
format Millis Into Minutes And Seconds
return String.format("%d min, %d sec", TimeUnit.MILLISECONDS.toMinutes(millis),
        TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
StringformatMs(long millis)
format Ms
return String.format("%02d:%02d:%02d.%03d", TimeUnit.MILLISECONDS.toHours(millis),
        TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
        TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1), millis % 1000);
StringformatSeconds(int seconds)
format Seconds
int millis = seconds * 1000;
long min = TimeUnit.MILLISECONDS.toMinutes(millis);
long sec = TimeUnit.MILLISECONDS.toSeconds(millis)
        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis));
if ((int) sec == 0 && min > 0) {
    if ((int) min == 1) {
        return min + " minute";
    } else {
...
StringformatTime(long time)
Formats given time in hh:ss or HH:mm:ss format, depending on whether time is longer then hour or not.
String result = null;
long hr = TimeUnit.MILLISECONDS.toHours(time);
long min = TimeUnit.MILLISECONDS.toMinutes(time - TimeUnit.HOURS.toMillis(hr));
long sec = TimeUnit.MILLISECONDS
        .toSeconds(time - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
if (hr == 0) {
    result = String.format(FMT_NO_HR, min, sec);
} else {
...