Example usage for java.util.concurrent TimeUnit convert

List of usage examples for java.util.concurrent TimeUnit convert

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit convert.

Prototype

public long convert(long sourceDuration, TimeUnit sourceUnit) 

Source Link

Document

Converts the given time duration in the given unit to this unit.

Usage

From source file:com.jkoolcloud.tnt4j.streams.utils.TimestampFormatter.java

/**
 * Converts the specified numeric timestamp from one precision to another.
 *
 * @param timestamp/* w ww .  ja v a  2  s  .  c om*/
 *            numeric timestamp to convert
 * @param fromUnits
 *            precision units timestamp is in
 * @param toUnits
 *            precision units to convert timestamp to
 * @return converted numeric timestamp in precision units specified by toUnits
 */
public static long convert(Number timestamp, TimeUnit fromUnits, TimeUnit toUnits) {
    return toUnits.convert(timestamp == null ? 0 : timestamp.longValue(), fromUnits);
}

From source file:de.phillme.PhotoSorter.java

/**
 * Get a diff between two dates//from   w  w w  .  j a v  a2s. c om
 *
 * @param date1    the oldest date
 * @param date2    the newest date
 * @param timeUnit the unit in which you want the diff
 * @return the diff value, in the provided unit
 */
private static long getDateDiff(Date date1, Date date2, TimeUnit timeUnit) {
    long diffInMillies = date2.getTime() - date1.getTime();
    return timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS);
}

From source file:org.apache.spark.network.util.JavaUtils.java

/**
 * Convert a passed time string (e.g. 50s, 100ms, or 250us) to a time count in the given unit.
 * The unit is also considered the default if the given string does not specify a unit.
 *//*w w  w .  ja v a2s  .  c  o  m*/
public static long timeStringAs(String str, TimeUnit unit) {
    String lower = str.toLowerCase().trim();

    try {
        Matcher m = Pattern.compile("(-?[0-9]+)([a-z]+)?").matcher(lower);
        if (!m.matches()) {
            throw new NumberFormatException("Failed to parse time string: " + str);
        }

        long val = Long.parseLong(m.group(1));
        String suffix = m.group(2);

        // Check for invalid suffixes
        if (suffix != null && !timeSuffixes.containsKey(suffix)) {
            throw new NumberFormatException("Invalid suffix: \"" + suffix + "\"");
        }

        // If suffix is valid use that, otherwise none was provided and use the default passed
        return unit.convert(val, suffix != null ? timeSuffixes.get(suffix) : unit);
    } catch (NumberFormatException e) {
        String timeError = "Time must be specified as seconds (s), "
                + "milliseconds (ms), microseconds (us), minutes (m or min), hour (h), or day (d). "
                + "E.g. 50s, 100ms, or 250us.";

        throw new NumberFormatException(timeError + "\n" + e.getMessage());
    }
}

From source file:DelayedJob.java

@Override
public long getDelay(TimeUnit unit) {
    long delay = MILLIS.between(Instant.now(), scheduledTime);
    long returnValue = unit.convert(delay, MILLISECONDS);
    return returnValue;
}

From source file:org.apache.hadoop.hbase.master.RegionServerOperation.java

public long getDelay(TimeUnit unit) {
    return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}

From source file:org.apache.storm.loadgen.LoadMetricsServer.java

@VisibleForTesting
static double convert(double value, TimeUnit from, TimeUnit target) {
    if (target.compareTo(from) > 0) {
        return value / from.convert(1, target);
    }//from  w  w  w.  j  av a 2  s  .co m
    return value * target.convert(1, from);
}

From source file:com.jms.notify.core.NotifyTask.java

public long getDelay(TimeUnit unit) {
    return unit.convert(executeTime - System.currentTimeMillis(), unit.SECONDS);
}

From source file:com.civprod.util.concurrent.locks.CompositeLock.java

@Override
public boolean tryLock(long time, TimeUnit unit) throws InterruptedException {
    long timeInNanos = unit.convert(time, TimeUnit.NANOSECONDS);
    long startTime = System.nanoTime();
    int currentLockNumber = 0;
    boolean locked = true;
    try {//from  w ww .  j a  v a2 s.co  m
        while (locked && (currentLockNumber < interLocks.size())) {
            long currentWaitTime = timeInNanos - (System.nanoTime() - startTime);
            if (interLocks.get(currentLockNumber).tryLock(currentWaitTime, TimeUnit.NANOSECONDS)) {
                currentLockNumber++;
            } else {
                locked = false;
            }
        }
    } catch (Exception ex) {
        locked = false;
        throw (ex);
    } finally {
        if (!locked) {
            while (currentLockNumber >= 1) {
                currentLockNumber--;
                interLocks.get(currentLockNumber).unlock();
            }
        }
    }
    return locked;
}

From source file:org.openehealth.ipf.commons.test.performance.Timestamp.java

/**
 * Returns the timestamp value in the given unit
 * // w w  w  . j a va2s.  c o m
 * @param unit
 * @return
 */
public long getValue(TimeUnit unit) {
    notNull(unit, "The unit must not be null!");
    return unit.convert(nanoValue, NANO_UNIT);
}

From source file:at.ac.univie.isc.asio.tool.Timeout.java

/**
 * Get the timeout value converted to the given time-unit, or the fallback value, if the timeout
 * is not defined./*from  w w  w.j  a v a2 s  .  com*/
 * @param unit target time-unit
 * @param fallback default value
 * @return literal value of timeout in the target unit
 */
public long getAs(@Nonnull final TimeUnit unit, final long fallback) {
    return defined ? unit.convert(timeoutInNanos, TimeUnit.NANOSECONDS) : fallback;
}