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

intgetDayFromTimestamp(final long unixTimestamp)
get Day From Timestamp
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(TimeUnit.MILLISECONDS.convert(unixTimestamp, TimeUnit.SECONDS));
int day = cal.get(Calendar.DAY_OF_WEEK) - 1;
return day;
longgetDays(final long ms)
get Days
return TimeUnit.MILLISECONDS.toDays(ms);
ListgetDays(long endTs, Long lookback)
get Days
long to = midnightUTC(endTs);
long from = midnightUTC(endTs - (lookback != null ? lookback : endTs));
List<Date> days = new ArrayList<>();
for (long time = from; time <= to; time += TimeUnit.DAYS.toMillis(1)) {
    days.add(new Date(time));
return days;
StringgetDeltaBetweenTimestamps(final long start, final long end)
Provides delta between timestamps in format "X Days XXhr XXm XXs".
if (start < 0 || end < 0 || start > end) {
    throw new IllegalArgumentException("Wrong timestamp values: " + start + " or " + end);
TimeUnit timeUnit = TimeUnit.SECONDS;
long diffInMilliseconds = end - start;
long s = timeUnit.convert(diffInMilliseconds, TimeUnit.MILLISECONDS);
long days = s / (24 * 60 * 60);
long rest = s - (days * 24 * 60 * 60);
...
longgetDifferenceDays(Date d1, Date d2)
get Difference Days
long diff = d2.getTime() - d1.getTime();
return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
longgetDiffInMinutes(Calendar startTime, Calendar endTime)
Returns the difference of two calendars in minutes
long diffMillis = Math.abs(endTime.getTimeInMillis() - startTime.getTimeInMillis());
return TimeUnit.MILLISECONDS.toMinutes(diffMillis);
StringgetDurationBreakdown(long millis)
get Duration Breakdown
String[] units = { " Days ", " Hours ", " Minutes ", " Seconds " };
Long[] values = new Long[units.length];
if (millis < 0) {
    throw new IllegalArgumentException("Duration must be greater than zero!");
values[0] = TimeUnit.MILLISECONDS.toDays(millis);
millis -= TimeUnit.DAYS.toMillis(values[0]);
values[1] = TimeUnit.MILLISECONDS.toHours(millis);
...
StringgetDurationBreakdown(long millis)
Convert a millisecond duration to a string format
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.MILLISECONDS.toMillis(minutes);
...
StringgetDurationBreakdown(long millis)
Convert a millisecond duration to a string format
long ms = millis;
if (ms < 0)
    return "ASAP";
long days = TimeUnit.MILLISECONDS.toDays(ms);
ms -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(ms);
ms -= TimeUnit.HOURS.toMillis(hours);
long minutes = TimeUnit.MILLISECONDS.toMinutes(ms);
...
StringgetDurationBreakdown(long millis)
get Duration Breakdown
long time = millis;
if (time < 0) {
    throw new IllegalArgumentException("Duration must be greater than zero!");
long days = TimeUnit.MILLISECONDS.toDays(time);
time -= TimeUnit.DAYS.toMillis(days);
long hours = TimeUnit.MILLISECONDS.toHours(time);
time -= TimeUnit.HOURS.toMillis(hours);
...