Java TimeUnit Convert toMillis(final long interval, final TimeUnit tu)

Here you can find the source of toMillis(final long interval, final TimeUnit tu)

Description

Convert time interval to milliseconds.

License

Open Source License

Parameter

Parameter Description
interval a parameter
tu a parameter

Return

Interval in milliseconds

Declaration

private static long toMillis(final long interval, final TimeUnit tu) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.concurrent.TimeUnit;

public class Main {
    /**//from w ww . ja v a  2s  .  c o m
     * Convert time interval to milliseconds.
     *
     * @param interval
     * @param tu
     *
     * @return Interval in milliseconds
     */
    private static long toMillis(final long interval, final TimeUnit tu) {
        if (interval < 0L) {
            throw new IllegalArgumentException("SleepUtilities.toMillis " + "Negative interval");
        }

        long millis = 0L;

        switch (tu) {
        // case DAYS:   Wait for 1.6
        // case HOURS:
        // case MINUTES:
        case SECONDS:
            millis = tu.toMillis(interval);
            break;

        case MILLISECONDS:
            millis = interval;
            break;

        case MICROSECONDS:
            millis = (interval + 500L) / 1000L;
            break;

        case NANOSECONDS:
            millis = (interval + 500000L) / 1000000L;
            break;

        default:
            throw new IllegalArgumentException("SleepUtilities.toMillis " + "Unknown TimeUnit: " + tu);
        }

        if ((interval > 0L) && (millis == 0L)) {
            // Don't underflow
            millis = 1L;
        }

        return millis;
    }
}

Related

  1. stringToTimeUnit(String str)
  2. substract(Date date, long durationToSubstract, TimeUnit unit)
  3. timeToNanoSeconds(TimeUnit unit, long time)
  4. toIntervalFromNow(long timeback, TimeUnit unit)
  5. toMillis(Double sourceValue, TimeUnit sourceUnit)
  6. toMillis(int duration, TimeUnit unit)
  7. toMillis(long duration, TimeUnit timeUnit)
  8. toMillis(long duration, TimeUnit unit)
  9. toMillis(long time, TimeUnit unit)