I'm trying to write a random date of birth generator for a date of birth between 25 and 50 years ago. I'm stuck with Java 1.4 and am attempting to accomlish this via java.util.Calendar (yes, yes, I know I should be using the Joda libraries). Here's where I'm at:
private static long ONE_YEAR_AS_MILLISECONDS = 365*24*60*60*1000;
private static long TWENTY_FIVE_YEARS_AS_MILLISECONDS = 25*ONE_YEAR_AS_MILLISECONDS;
private static long FIFTY_YEARS_AS_MILLISECONDS = 50*ONE_YEAR_AS_MILLISECONDS;
private static String generateDOB()
{
//Equation for calculating a random number within a given range is as follows: Min + (int)(Math.random() * ((Max - Min) + 1))
long someTimeBetween25And50YearsInMilliSeconds = TWENTY_FIVE_YEARS_AS_MILLISECONDS +
(long)(Math.random() * ((FIFTY_YEARS_AS_MILLISECONDS - TWENTY_FIVE_YEARS_AS_MILLISECONDS) + 1));
Calendar dob = Calendar.getInstance();
dob.setTimeInMillis(dob.getTimeInMillis() - someTimeBetween25And50YearsInMilliSeconds);
StringBuffer sb = new StringBuffer();
sb.append(dob.get(Calendar.YEAR)).append("/").append(dob.get(Calendar.MONTH)+1).append("/").append(dob.get(Calendar.DAY_OF_MONTH));
return sb.toString();
}
It executes OK, but an output of 100 dates yeilds all of them in either 2008 or 2009:
2008/10/8
2008/9/22
2008/7/26
2008/8/20
2008/3/31
2008/10/1
2009/1/3
2009/2/2
2009/4/9
2008/9/20
...
However, I need dates between 25-50 years ago. What am I missing?