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

StringformatTime(long time)
format Time
long days = TimeUnit.SECONDS.toDays(time) % 30;
long hours = TimeUnit.SECONDS.toHours(time) % 24;
long minutes = TimeUnit.SECONDS.toMinutes(time) % 60;
long seconds = TimeUnit.SECONDS.toSeconds(time) % 60;
return (days == 0L) ? String.format("%d hours, %d minutes; and %d seconds", hours, minutes, seconds)
        : String.format("%d days, %d hours, %d minutes; and %d seconds", days, hours, minutes, seconds);
StringformatTime(long timeInNanoseconds)
format Time
long minutes = TimeUnit.NANOSECONDS.toMinutes(timeInNanoseconds);
long secondes = TimeUnit.NANOSECONDS.toSeconds(timeInNanoseconds) - TimeUnit.MINUTES.toSeconds(minutes);
long ms = TimeUnit.NANOSECONDS.toMillis(timeInNanoseconds) - TimeUnit.MINUTES.toMillis(minutes)
        - TimeUnit.SECONDS.toMillis(secondes);
StringBuilder builder = new StringBuilder();
if (minutes > 0) {
    builder.append(minutes);
    builder.append(" min ");
...
StringformatTime(String format, double nanoseconds)
format Time
return formatTime(format, nanoseconds, TimeUnit.NANOSECONDS);
StringformatTimeDuration(long duration)
This method is used to format a timestamp to 'dd:hh:mm:ss'.
String timeDuration;
long days = TimeUnit.MILLISECONDS.toDays(duration);
long hours = TimeUnit.MILLISECONDS.toHours(duration)
        - TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration)
        - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration)
        - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
...
StringformatTimeInMilliSeconds(long duration)
Formats the time into mill
try {
    StringBuffer durationInString = new StringBuffer("");
    long minutes = TimeUnit.MILLISECONDS.toSeconds(duration) / 60;
    long seconds = TimeUnit.MILLISECONDS.toSeconds(duration - minutes * 60 * 1000);
    long milliseconds = duration - (minutes * 60 * 1000) - seconds * 1000;
    if (minutes > 0) {
        durationInString.append(minutes).append(" min ");
    if (seconds > 0) {
        durationInString.append(seconds).append(" sec ");
    if (milliseconds > 0) {
        durationInString.append(milliseconds).append(" msec");
    return durationInString.toString();
} catch (Exception e) {
    e.printStackTrace();
    return "-1";
StringformatUptime(long startTime)
format Uptime
long seconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime);
final long days = seconds / TimeUnit.DAYS.toSeconds(1);
seconds -= TimeUnit.DAYS.toSeconds(days);
final long hours = seconds / TimeUnit.HOURS.toSeconds(1);
seconds -= TimeUnit.HOURS.toSeconds(hours);
final long minutes = seconds / TimeUnit.MINUTES.toSeconds(1);
seconds -= TimeUnit.MINUTES.toSeconds(minutes);
return String.format("%dd:%dh:%dm:%ds", days, hours, minutes, seconds);
...
StringformatUptime(long uptimeInMs)
format Uptime
long remainingMs = uptimeInMs;
long days = TimeUnit.MILLISECONDS.toDays(remainingMs);
remainingMs -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(remainingMs);
remainingMs -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(remainingMs);
remainingMs -= TimeUnit.MINUTES.toMillis(minutes);
long seconds = TimeUnit.MILLISECONDS.toSeconds(remainingMs);
...
DategenerateRandomDateTimeForLogEvent(Date laterThan)
generate Random Date Time For Log Event
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
if (laterThan == null) {
    cal.add(GregorianCalendar.YEAR, -1);
    cal.add(GregorianCalendar.MONTH, (int) (Math.round(Math.random() * 12)) * -1);
    cal.add(GregorianCalendar.WEEK_OF_MONTH, (int) (Math.round(Math.random() * 4)) * -1);
    cal.add(GregorianCalendar.DAY_OF_WEEK, (int) (Math.round(Math.random() * 7)) * -1);
    laterThan = cal.getTime();
long randomAdditionalTime = (long) (Math.round(Math.random() * TimeUnit.DAYS.toMillis(1)));
cal.setTimeInMillis(laterThan.getTime() + randomAdditionalTime);
return cal.getTime();
intgetAge(Date birthday)
get Age
long day = TimeUnit.DAYS.convert(System.currentTimeMillis() - birthday.getTime(), TimeUnit.MILLISECONDS);
return (int) (day / 365);
longgetAge(long currentTimeMillis, long headerTimestampSec)
Returns the age of the GTFS-realtime feed, based on the provided current time and GTFS-realtime header time, in milliseconds
long headerTimeMillis = TimeUnit.SECONDS.toMillis(headerTimestampSec);
return currentTimeMillis - headerTimeMillis;