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

intgetMinutesBetweenDates(Date firstDate, Date secondDate)
get Minutes Between Dates
long differenceInMillis = secondDate.getTime() - firstDate.getTime();
return (int) TimeUnit.MILLISECONDS.toMinutes(differenceInMillis);
longgetNormalizedIntervalStartTimestamp(long intervalLengthMillis, long currentTimestamp)
get Normalized Interval Start Timestamp
long intervalStartTimestamp;
if (intervalLengthMillis >= 3600000) {
    Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
    cal.setTimeInMillis(currentTimestamp);
    cal.set(Calendar.MINUTE, 0);
    cal.set(Calendar.SECOND, 0);
    cal.set(Calendar.MILLISECOND, 0);
    intervalStartTimestamp = cal.getTimeInMillis();
...
longgetNowInSeconds()
Return the current time in seconds.
return getNowTimeUnit(TimeUnit.SECONDS);
intgetOceanTime(final Date date)
Convert the specified date to an Ocean time.
return (int) TimeUnit.SECONDS.convert(date.getTime(), TimeUnit.MILLISECONDS) - NO_SECONDS_1970_1980;
StringgetPrettyTime(long duration)
get Pretty Time
if (duration <= 0)
    throw new IllegalArgumentException();
return String.format("%02d:%02d:%02d.%03d", TimeUnit.MILLISECONDS.toHours(duration),
        TimeUnit.MILLISECONDS.toMinutes(duration)
                - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration)),
        TimeUnit.MILLISECONDS.toSeconds(duration)
                - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration)),
        duration - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration)));
...
StringgetQuantilesString(final Map quantilesValues)
get Quantiles String
final StringBuilder builder = new StringBuilder();
for (final Entry<Double, Long> entry : quantilesValues.entrySet()) {
    final String quantile = (entry.getKey() * 100) + " % : "
            + TimeUnit.NANOSECONDS.toNanos(entry.getValue()) + " ns";
    builder.append(quantile);
    builder.append("\n");
return builder.toString();
...
DategetRandomDate()
get Random Date
long time = (long) (System.currentTimeMillis() - getRandomDouble() * DAYS.toMillis(3650));
time = time / 1000 * 1000;
return new Date(time);
StringgetReadableElapsedIntervalInSeconds(final long elapsedTimInMillis)
get Readable Elapsed Interval In Seconds
final long hours = TimeUnit.MILLISECONDS.toHours(elapsedTimInMillis);
final long minutes = TimeUnit.MILLISECONDS.toMinutes(elapsedTimInMillis - TimeUnit.HOURS.toMillis(hours));
final long seconds = TimeUnit.MILLISECONDS
        .toSeconds(elapsedTimInMillis - TimeUnit.HOURS.toMillis(hours) - TimeUnit.MINUTES.toMillis(minutes))
        + (hours * 60 * 60) + (minutes * 60);
final long milliSeconds = TimeUnit.MILLISECONDS.toMillis(elapsedTimInMillis - TimeUnit.HOURS.toMillis(hours)
        - TimeUnit.MINUTES.toMillis(minutes) - TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(
                elapsedTimInMillis - TimeUnit.HOURS.toMillis(hours) - TimeUnit.MINUTES.toMillis(minutes))));
...
StringgetRemainingTimeString(int completedCount, int totalCount, long elapsed)
get Remaining Time String
float conceptCountFloat = totalCount;
float completedFloat = completedCount;
float percentComplete = completedFloat / conceptCountFloat;
float estTotalTime = elapsed / percentComplete;
long remaining = (long) (estTotalTime - elapsed);
String remainingStr = getElapsedTimeString(remaining);
return remainingStr;
doublegetSeconds(long time)
Return the seconds since the provided time.
return (double) TimeUnit.MILLISECONDS.toSeconds(time - System.currentTimeMillis());