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

longminutesSince(final long startNanos)
minutes Since
return timeSince(startNanos, TimeUnit.MINUTES);
StringnanoElapseToHumanReadable(final Long nanoElapse)
nano Elapse To Human Readable
if (nanoElapse == null) {
    return "not started";
long nanoElapseLocal = nanoElapse;
String elapseFormat = null;
final long day = TimeUnit.NANOSECONDS.toDays(nanoElapseLocal);
nanoElapseLocal -= TimeUnit.DAYS.toNanos(day);
if (day > 0) {
...
StringnanosToHMSms(long nanos)
nanos To HM Sms
long hour = TimeUnit.NANOSECONDS.toHours(nanos);
long in1 = nanos - TimeUnit.HOURS.toNanos(hour);
long minute = TimeUnit.NANOSECONDS.toMinutes(in1);
long in2 = in1 - TimeUnit.MINUTES.toNanos(minute);
long second = TimeUnit.NANOSECONDS.toSeconds(in2);
long in3 = in2 - TimeUnit.SECONDS.toNanos(second);
long milli = TimeUnit.NANOSECONDS.toMillis(in3);
long in4 = in3 - TimeUnit.MILLISECONDS.toNanos(milli);
...
longnanosToSecs(long nanos)
nanos To Secs
return TimeUnit.NANOSECONDS.toSeconds(nanos);
StringnanoToString(final long nanos)
nano To String
final TimeUnit unit = chooseUnit(nanos);
final double value = (double) nanos / NANOSECONDS.convert(1, unit);
return String.format("%.4g %s", value, abbreviate(unit));
longnow()
now
Date date = new Date();
long b = TimeUnit.MILLISECONDS.toSeconds(date.getTime());
return b;
longnumberOfDaysSinceEpoch(long milliSinceEpoch)
Returns the number of days since epoch with respect to the given number of milliseconds since epoch.
return TimeUnit.MILLISECONDS.toDays(milliSinceEpoch);
intparseDate(String value)
parse Date
return (int) TimeUnit.MILLISECONDS.toDays(DATE_FORMATTER.parseMillis(value));
longparseDuration(String durationStr)
Parses a duration String to its long value.
Preconditions.checkArgument(!Strings.isNullOrEmpty(durationStr));
durationStr = durationStr.toLowerCase();
String value = durationStr.substring(0, durationStr.length() - 1);
int parsedValue = 0;
try {
    parsedValue = Integer.parseInt(value);
} catch (NumberFormatException nfe) {
    Throwables.propagate(nfe);
...
longparseDuration(String text)
parse Duration
long result = 0;
int numIdx = 0;
for (int i = 0; i < text.length(); i++) {
    char at = text.charAt(i);
    if (at == 'd' || at == 'w' || at == 'm' || at == 'h') {
        String ns = text.substring(numIdx, i);
        numIdx = i + 1;
        if (ns.isEmpty()) {
...