Java TimeUnit Convert toMillis(long time, TimeUnit unit)

Here you can find the source of toMillis(long time, TimeUnit unit)

Description

Convert the specified duration expressed in the specfied time unit into milliseconds.

License

Apache License

Parameter

Parameter Description
time the duration to convert.
unit the unit in which the duration is expressed.

Return

the duration coverted to milliseconds.

Declaration

public static long toMillis(long time, TimeUnit unit) 

Method Source Code

//package com.java2s;
/*/*  w  w  w .jav a2  s.c o  m*/
 * JPPF.
 * Copyright (C) 2005-2010 JPPF Team.
 * http://www.jppf.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Convert the specified duration expressed in the specfied time unit into milliseconds.
     * If the unit is smaller than a millisecond (either {@link java.util.concurrent.TimeUnit#NANOSECONDS TimeUnit.NANOSECONDS} or 
     * {@link java.util.concurrent.TimeUnit#MICROSECONDS TimeUnit.MICROSECONDS}), the result will be rounded to the closest millisecond.
     * @param time the duration to convert.
     * @param unit the unit in which the duration is expressed.
     * @return the duration coverted to milliseconds.
     */
    public static long toMillis(long time, TimeUnit unit) {
        long millis = TimeUnit.MILLISECONDS.equals(unit) ? time : unit
                .convert(time, TimeUnit.MILLISECONDS);
        long remainder = 0L;
        if (TimeUnit.NANOSECONDS.equals(unit)) {
            remainder = time % 1000000L;
            if (remainder >= 500000L)
                millis++;
        } else if (TimeUnit.MICROSECONDS.equals(unit)) {
            remainder = time % 1000L;
            if (remainder >= 500L)
                millis++;
        }
        return millis;
    }
}

Related

  1. toMillis(Double sourceValue, TimeUnit sourceUnit)
  2. toMillis(final long interval, final TimeUnit tu)
  3. toMillis(int duration, TimeUnit unit)
  4. toMillis(long duration, TimeUnit timeUnit)
  5. toMillis(long duration, TimeUnit unit)
  6. toMillis(String timeUnitString, String timeValue)
  7. toNanos(long timeout, TimeUnit unit)
  8. toSeconds(long duration, TimeUnit timeUnit)
  9. toSeconds(long timeout, TimeUnit unit)