Here you can find the source of getRandomTimeBetweenTwoDates(Timestamp beginDate, Map
private static long getRandomTimeBetweenTwoDates(Timestamp beginDate, Map<String, Object> context)
//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; } }