Java TimeUnit Calculate getRandomTimeRound(int _duration, TimeUnit _unit)

Here you can find the source of getRandomTimeRound(int _duration, TimeUnit _unit)

Description

get random time in druation whit time unit from now.

License

Open Source License

Parameter

Parameter Description
_duration a parameter
_unit a parameter

Declaration

private static Date getRandomTimeRound(int _duration, TimeUnit _unit) 

Method Source Code

//package com.java2s;
/**//from   w  w  w  .j av  a2s.  c  om
 * PureInfo Peach2
 * @(#)QuartzJobUtil.java   1.0 2008-12-19
 * 
 * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. 
 * All rights reserved, see the license file.
 * 
 * www.pureinfo.com.cn
 */

import java.util.Date;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * get random time in druation whit time unit from now.
     * 
     * @param _duration
     * @param _unit
     * @return
     */
    private static Date getRandomTimeRound(int _duration, TimeUnit _unit) {
        if (_duration < 0) {
            return new Date();
        }
        int i = new Random().nextInt(_duration);
        return new Date(new Date().getTime() + TimeUnit.MILLISECONDS.convert(i, _unit));
    }
}

Related

  1. getIntervalInfo(long intervalDuration, TimeUnit unit)
  2. getMilliseconds(int interval, TimeUnit unit)
  3. getNowTimeUnit(TimeUnit timeUnit)
  4. getParentUnit(TimeUnit unit)
  5. getProperUnitName(TimeUnit unit, long amount)
  6. getTimeBucket(TimeUnit unit, long timestamp, int bucketSizeInSeconds)
  7. getTimeInMillis(TimeUnit unit)
  8. getTimeString(long value, TimeUnit unit)
  9. getTimeUnit(Object units)