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

StringmillisToHMSms(long millis)
millis To HM Sms
long hour = TimeUnit.MILLISECONDS.toHours(millis);
long in1 = millis - TimeUnit.HOURS.toMillis(hour);
long minute = TimeUnit.MILLISECONDS.toMinutes(in1);
long in2 = in1 - TimeUnit.MINUTES.toMillis(minute);
long second = TimeUnit.MILLISECONDS.toSeconds(in2);
long in3 = in2 - TimeUnit.SECONDS.toMillis(second);
return String.format("%dh:%dm:%ds:%dms", hour, minute, second, in3);
StringmillisToReadableTime(long milliseconds)
millis To Readable Time
long millis = milliseconds;
long days = TimeUnit.MILLISECONDS.toDays(millis);
millis -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
...
StringmillisToString(double millis)
millis To String
long seconds = TimeUnit.MILLISECONDS.toSeconds((long) millis);
long hours = seconds / 3600;
long minutes = (seconds % 3600) / 60;
long remaining = (seconds % 3600) % 60;
String output = "";
if (hours > 0) {
    output = output.concat(String.format("%02d:", hours));
output = output.concat(String.format("%02d:", minutes));
output = output.concat(String.format("%02d", remaining));
return output;
StringmillisToString(long millis)
millis To String
long hours = TimeUnit.MILLISECONDS.toHours(millis);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis) - (hours * 60);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis) - ((hours * 60 * 60) + (minutes * 60));
millis = millis - ((hours * 60 * 60 * 1000) + (minutes * 60 * 1000) + (seconds * 1000));
return String.format("%d hr(s) %d min(s) %d sec(s) %d ms", hours, minutes, seconds, millis);
StringmillisToString(long ms)
millis To String
if (ms <= 0) {
    throw new IllegalArgumentException("Input must be greater than zero");
long hours = TimeUnit.MILLISECONDS.toHours(ms);
ms -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(ms);
ms -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(ms);
...
StringmillisToStringConvert(long millis)
millis To String Convert
return String.format(Locale.getDefault(), "%d min, %d sek", TimeUnit.MILLISECONDS.toMinutes(millis),
        TimeUnit.MILLISECONDS.toSeconds(millis)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
StringmillisToTime(long millis)
millis To Time
long h = TimeUnit.HOURS.convert(millis, TimeUnit.MILLISECONDS);
long m = TimeUnit.MINUTES.convert(millis, TimeUnit.MILLISECONDS) - h * 60;
long s = TimeUnit.SECONDS.convert(millis, TimeUnit.MILLISECONDS) - h * 3600 - m * 60;
String sh = Long.toString(h);
String sm = Long.toString(m);
String ss = Long.toString(s);
if (sm.length() < 2)
    sm = "0" + sm;
...
StringmillisToTimeDelta(long millis)
millis To Time Delta
long days = TimeUnit.MILLISECONDS.toDays(millis);
millis -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(millis);
millis -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
millis -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
millis -= TimeUnit.SECONDS.toMillis(seconds);
...
TimeUnitmin()
min
return TimeUnit.values()[0];
longminutesBetween(Date date1, Date date2)
minutes Between
long diffInMillies = date2.getTime() - date1.getTime();
return TimeUnit.MINUTES.convert(diffInMillies, TimeUnit.MILLISECONDS);