List of utility methods to do TimeUnit Usage
String | convertNanosecondTimespanToHumanReadableFormat(long aTimespan, boolean aShortFormat, boolean aLongFormat) Converts a duration in nanoseconds to a human-readable, nicely formatted string. if (aTimespan < TimeUnit.SECONDS.toMillis(1)) { return aTimespan + (aShortFormat ? "ms" : aLongFormat ? " milliseconds" : "ms"); } else { StringBuilder tempBuilder = new StringBuilder(); if (aTimespan >= TimeUnit.DAYS.toNanos(1)) { tempBuilder.append(TimeUnit.NANOSECONDS.toDays(aTimespan) + (aShortFormat ? "d" : aLongFormat ? " days" : " days")); if (aTimespan >= TimeUnit.HOURS.toNanos(1)) { if (tempBuilder.length() > 0) { tempBuilder.append(" "); tempBuilder.append(TimeUnit.NANOSECONDS.toHours(aTimespan % TimeUnit.DAYS.toNanos(1)) + (aShortFormat ? "h" : aLongFormat ? " hours" : "hrs")); if (aTimespan >= TimeUnit.MINUTES.toNanos(1)) { if (tempBuilder.length() > 0) { tempBuilder.append(" "); tempBuilder.append(TimeUnit.NANOSECONDS.toMinutes(aTimespan % TimeUnit.HOURS.toNanos(1)) + (aShortFormat ? "m" : aLongFormat ? " minutes" : "min")); if (aTimespan >= TimeUnit.SECONDS.toNanos(1)) { if (tempBuilder.length() > 0) { tempBuilder.append(" "); tempBuilder.append(TimeUnit.NANOSECONDS.toSeconds(aTimespan % TimeUnit.MINUTES.toNanos(1)) + (aShortFormat ? "s" : aLongFormat ? " seconds" : "sec")); if (aTimespan >= TimeUnit.MILLISECONDS.toNanos(1) && aTimespan < TimeUnit.MINUTES.toNanos(1)) { if (tempBuilder.length() > 0) { tempBuilder.append(" "); tempBuilder.append(TimeUnit.NANOSECONDS.toMillis(aTimespan % TimeUnit.SECONDS.toNanos(1)) + (aShortFormat ? "ms" : aLongFormat ? " milliseconds" : "msecs")); return tempBuilder.toString(); |
long | convertNanoToSeconds(long nanoTime) Just for convenience, since will be used a lot return TimeUnit.SECONDS.convert(nanoTime, TimeUnit.NANOSECONDS);
|
String | createTimeString(int year, int month, int day, int hours, int minutes, int seconds, String timezoneID) Creates a timestamp conform to the ISO 8601 specification, supported its single information bits. StringBuilder builder = new StringBuilder(); builder.append(Integer.toString(year)).append("-").append(Integer.toString(month)).append("-") .append(Integer.toString(day)).append("T"); if (hours < 10) { builder.append(0); builder.append(Integer.toString(hours)); builder.append(":"); ... |
String | createTimezoneString(String timezoneID) Taking a timezone ID, this method returns the offset in hours. StringBuilder builder = new StringBuilder(); DateTimeZone timeZone = DateTimeZone.forID(timezoneID); long offsetInMilliseconds = timeZone.toTimeZone().getRawOffset(); long offsetHours = TimeUnit.MILLISECONDS.toHours(offsetInMilliseconds); if (offsetHours == 0) { builder.append("Z"); } else { if (offsetHours < 0) { ... |
long | currentMillisFromNanotime() System#nanoTime() is less expensive than System#currentTimeMillis() and better suited to measure time intervals. return TimeUnit.MILLISECONDS.convert(System.nanoTime(), TimeUnit.NANOSECONDS);
|
Long | dateDiff(Date d1, Date d2) Returns the number of days between d1 and d2. long diff = d2.getTime() - d1.getTime(); return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS); |
Calendar | dateStart(final Calendar c) date Start c.set(Calendar.HOUR_OF_DAY, 0);
c.set(Calendar.MINUTE, 0);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
return c;
|
long | daysBetween(Calendar startDate, Calendar endDate) days Between long end = endDate.getTimeInMillis(); long start = startDate.getTimeInMillis(); return TimeUnit.MILLISECONDS.toDays(Math.abs(end - start)); |
long | daysBetween(Date initDate, Date endDate) days Between return daysBetween(initDate, endDate, true);
|
int | daysBetweenDates(long toDate, long fromDate) number of days between two dates. return (int) TimeUnit.DAYS.convert(toDate - fromDate, TimeUnit.MILLISECONDS); |