Generating random Date/Time between 2 different Dates/TimeStamps in Java
It is common to find a random number between 2 given numbers… In our java application that we are working on, We had a need to find a random time between two given timestamp.. We felt it little challenging in the beginning… But later figured out a simple solution for the same… The steps for generating a random time between 2 timestamps really turned out to be very simple & this article is about how we achieved that…
If min and max are the 2 variables that are holding minimum and maximum values, & random is the random number that is to be generated, in java the random number is calculated in this way…
random = (Math.random() * (max - min) ) + min
Here Math.random() is a function in java that generates any random value between 0 & 1… We multiply that with the difference between max & min values and finally add the obtained result with the min value to get a random number between 2 given numbers.. This idea was set as the basement for our requirement too… Wondering how?? This is how it goes…
We use Calendar object which defaults to current date and time. We then set Hour, minute & second in the same object(If required we can set the year, month & date too).. Now we have the Calendar object with the date what we set or current date and time. Calendar class has a method named getTimeInMillis which will convert this timeStamp to a long value. This long value serves as the first min variable as in the example where we generate a random number between 2 different numbers.
Now in the same way we get the long value of the second timestamp too which is the max variable value. Using a similar calculation as above we get a random long number between 2 different long numbers which are the long time. Finally we construct the date object with the long value that we obtained.. This date object’s value is the random time stamp between 2 different Calendar objects and hence the we can generate random date/time between two different Calendar objects!!!!
Here goes the code for that..
//code to generate random timestamp between morning 6 to evening 8 pm
public static void main(String args[]){
Calendar cdr = Calendar.getInstance();
cdr.set(Calendar.HOUR_OF_DAY, 6);
cdr.set(Calendar.MINUTE, 0);
cdr.set(Calendar.SECOND, 0);
long val1=cdr.getTimeInMillis();
cdr.set(Calendar.HOUR_OF_DAY, 20);
cdr.set(Calendar.MINUTE, 0);
cdr.set(Calendar.SECOND, 0);
long val2=cdr.getTimeInMillis();
Random r=new Random();
long randomTS=(long)(r.nextDouble()*(val2-val1))+val1;
Date d=new Date(randomTS);
System.out.println(d.toString());
}
PS: This will work even for generating random date between two different dates which can be a month/year gap.
Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.
Comments
That was an inspiring post,
I don’t know where I can use it ,but I loved it
Thanks for writing, most people don’t bother.
thanks for the idea.
I’m using it in my Mockup Data Generator, where I generate MockUp Data to test UI.
ekke
BTW: the UI is from http://redview.org, the generator is http://red-open.org – just publishing both as Open Source EPL


Nice work…cleaver use of the calendar object.