Java TimeUnit Calculate getNowTimeUnit(TimeUnit timeUnit)

Here you can find the source of getNowTimeUnit(TimeUnit timeUnit)

Description

Get the current time in the specified time unit.

License

Apache License

Parameter

Parameter Description
timeUnit a parameter

Declaration

public static long getNowTimeUnit(TimeUnit timeUnit) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.TimeUnit;

public class Main {
    /**//  w  w  w .  j ava 2s  .  co  m
     * Get the current time in the specified time unit.
     * @param timeUnit
     * @return
     */
    public static long getNowTimeUnit(TimeUnit timeUnit) {
        switch (timeUnit) {
        case NANOSECONDS:
            return TimeUnit.MILLISECONDS
                    .toNanos(System.currentTimeMillis());
        case MICROSECONDS:
            return TimeUnit.MILLISECONDS.toMicros(System
                    .currentTimeMillis());
        case MILLISECONDS:
            return TimeUnit.MILLISECONDS.toMillis(System
                    .currentTimeMillis());
            //default is seconds
        case SECONDS:
        default:
            return TimeUnit.MILLISECONDS.toSeconds(System
                    .currentTimeMillis());
        case MINUTES:
            return TimeUnit.MILLISECONDS.toMinutes(System
                    .currentTimeMillis());
        case HOURS:
            return TimeUnit.MILLISECONDS
                    .toHours(System.currentTimeMillis());
        case DAYS:
            return TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis());
        }
    }
}

Related

  1. getFragment(final Calendar calendar, final int fragment, final TimeUnit unit)
  2. getFutureDate(long delay, TimeUnit timeUnit)
  3. getInterval(final Date startDate, final Date endDate, final TimeUnit timeUnit)
  4. getIntervalInfo(long intervalDuration, TimeUnit unit)
  5. getMilliseconds(int interval, TimeUnit unit)
  6. getParentUnit(TimeUnit unit)
  7. getProperUnitName(TimeUnit unit, long amount)
  8. getRandomTimeRound(int _duration, TimeUnit _unit)
  9. getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)