Example usage for java.util.concurrent TimeUnit toMicros

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

Introduction

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

Prototype

public long toMicros(long duration) 

Source Link

Document

Equivalent to #convert(long,TimeUnit) MICROSECONDS.convert(duration, this) .

Usage

From source file:Main.java

public static void main(String[] args) {
    TimeUnit tu = TimeUnit.DAYS;

    System.out.println(tu.toDays(1));
    System.out.println(tu.toHours(1));
    System.out.println(tu.toMicros(1));

}

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

/**
 * Parses the value into a timestamp with microsecond accuracy based on the specified units.
 * <p>/*from  w  ww  . ja v a 2s.  c  o m*/
 * If {@code value} represents decimal number (as {@link Number} or {@link String}, fraction gets preserved by
 * scaling down {@code value} in {@code units} until numeric value expression gets with low (epsilon is
 * {@code 0.001}) or without fraction or {@code units} gets set to {@link TimeUnit#NANOSECONDS}.
 *
 * @param units
 *            units that value is in
 * @param value
 *            value to convert
 * @return microsecond timestamp
 * @throws ParseException
 *             if an error parsing the specified value
 *
 * @see #scale(double, TimeUnit)
 */
public static UsecTimestamp parse(TimeUnit units, Object value) throws ParseException {
    UsecTimestamp ts;
    try {
        long time;
        if (value instanceof Date) {
            time = ((Date) value).getTime();
            units = TimeUnit.MILLISECONDS;
        } else if (value instanceof Calendar) {
            time = ((Calendar) value).getTimeInMillis();
            units = TimeUnit.MILLISECONDS;
        } else {
            if (units == null) {
                units = TimeUnit.MILLISECONDS;
            }

            double dTime = value instanceof Number ? ((Number) value).doubleValue()
                    : Double.parseDouble(value.toString());

            Pair<Double, TimeUnit> sTimePair = scale(dTime, units);
            dTime = sTimePair.getLeft();
            units = sTimePair.getRight();

            time = (long) dTime;
        }

        switch (units) {
        case NANOSECONDS:
            long scale = 1000000L;
            long mSecs = time / scale;
            long uSecs = (time - mSecs * scale) / 1000L;
            ts = new UsecTimestamp(mSecs, uSecs);
            break;
        case MICROSECONDS:
            scale = 1000L;
            mSecs = time / scale;
            uSecs = time - mSecs * scale;
            ts = new UsecTimestamp(mSecs, uSecs);
            break;
        default:
            ts = new UsecTimestamp(units.toMicros(time));
            break;
        }
    } catch (NumberFormatException nfe) {
        ParseException pe = new ParseException(
                StreamsResources.getStringFormatted(StreamsResources.RESOURCE_BUNDLE_NAME,
                        "TimestampFormatter.failed.parsing", value, nfe.getLocalizedMessage()),
                0);
        pe.initCause(nfe);
        throw pe;
    }
    return ts;
}

From source file:org.apache.pulsar.client.impl.ConsumerBuilderImpl.java

@Override
public ConsumerBuilder<T> acknowledgmentGroupTime(long delay, TimeUnit unit) {
    checkArgument(delay >= 0, "acknowledgmentGroupTime needs to be >= 0");
    conf.setAcknowledgementsGroupTimeMicros(unit.toMicros(delay));
    return this;
}

From source file:org.apache.pulsar.client.impl.ProducerBuilderImpl.java

@Override
public ProducerBuilder<T> batchingMaxPublishDelay(long batchDelay, @NonNull TimeUnit timeUnit) {
    conf.setBatchingMaxPublishDelayMicros(timeUnit.toMicros(batchDelay));
    return this;
}