Java Timestamp Field getRandomTimeBetweenTwoDates(Timestamp beginDate, Map context)

Here you can find the source of getRandomTimeBetweenTwoDates(Timestamp beginDate, Map context)

Description

Method should generate random number that represents a time between two dates.

License

Apache License

Declaration

private static long getRandomTimeBetweenTwoDates(Timestamp beginDate, Map<String, Object> context) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.Timestamp;

import java.util.Calendar;

import java.util.List;
import java.util.Map;
import java.util.Random;

public class Main {
    /**/* w  w w. j  a  v  a2 s.  co m*/
    * Method should generate random number that represents a time between two
    * dates.
    * 
    * @return
    */
    private static long getRandomTimeBetweenTwoDates(Timestamp beginDate, Map<String, Object> context) {
        long beginTime, endTime;
        Calendar cal = Calendar.getInstance();

        if (context.get("maxDate") != null) {
            endTime = ((Timestamp) context.get("maxDate")).getTime();
        } else {
            endTime = cal.getTimeInMillis();
        }

        if (beginDate != null) {
            beginTime = beginDate.getTime();
        } else if (context.get("minDate") != null) {
            beginTime = ((Timestamp) context.get("minDate")).getTime();
        } else {
            cal.add(Calendar.DATE, -180);
            beginTime = cal.getTimeInMillis();
        }
        long diff = endTime - beginTime + 1;
        beginTime = beginTime + (long) (Math.random() * diff);
        return beginTime;
    }

    public static int random(List myList) {
        int size = myList.size();
        int index = new Random().nextInt(size);
        return index;
    }
}

Related

  1. getNowTimeStamp()
  2. getOffsetDaysTime(Timestamp sysDate, int offsetDays)
  3. getPerformanceTime(Timestamp startTime)
  4. getPeriodExpiration(Timestamp tsStartDate, int iPeriodType, int iPeriodDuration)
  5. getPresentTimeStamp()
  6. getRandTimestamp(Random r)
  7. getRealCalendar(Timestamp time)
  8. getResultRowValueAsTimestamp(String valueName, Map resultRow)
  9. getSafeLong(final Timestamp d, final long i)