views:

145

answers:

2

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?

+15  A: 

Quite a gotcha that problem, but you're getting a numeric overflow in your ONE_YEAR_AS_MILLISECONDS constant.

Change the integer literals to longs:

private static long ONE_YEAR_AS_MILLISECONDS = 365L * 24L * 60L * 60L * 1000L;

Works fine for me now.

EDIT:

Consequently, while I was working this out, I didn't use the Calendar class:

Date d = new Date(System.currentTimeMillis() - someTimeBetween25And50YearsInMilliSeconds);

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
return sdf.format(d);

That looks a bit easier to read to me.

Noel M
Great! That's a real gotcha. Definitly a +1!
Andreas_D
Oooh, nice spot! Thanks a bunch (and extra kudos for getting rid of Calendar)
Chris Knight
+3  A: 

I'm not sure what you are missing, but I would use Calendar.roll

private static String generateDOB()
{

    Calendar dob = Calendar.getInstance();

    int year = (int)(Math.random() * 25) + 25;


    dob.roll(Calendar.YEAR, -year);
    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();
}