0

I have the following HTML:

< input name="txtDDOB" tabIndex="7" id="txtDDOB" style="top: -2px; width: 128px; position: relative;" onblur="checkForDate()" type="text" range="1850-2050" format="%m/%d/%Y" value="01/01/1900"/>

I want to generate a random DOB. Kindly help me. I am using Java , selenium , IE 10, Windows 8.

Please also let me know which packages to import when using your code.Many thanks.

newLearner
  • 184
  • 2
  • 15

2 Answers2

1

Hi Use the following method. You need to provide the start and end years and it will generate the random data of birth.

private String randomDataOfBirth(int yearStart, int yearEnd)
    {
        GregorianCalendar gc = new GregorianCalendar();
        int year = randBetween(yearStart, yearEnd);
        gc.set(Calendar.YEAR, year);
         int dayOfYear = randBetween(1, gc.getActualMaximum(Calendar.DAY_OF_YEAR));

            gc.set(Calendar.DAY_OF_YEAR, dayOfYear);
            String date = null;
            if(gc.get(Calendar.MONTH) == 0)
            {
                 date = gc.get(Calendar.YEAR) + "-" + 1 + "-" + gc.get(Calendar.DAY_OF_MONTH);
            }else
            {
                 date = gc.get(Calendar.YEAR) + "-" + gc.get(Calendar.MONTH) + "-" + gc.get(Calendar.DAY_OF_MONTH);
            }
            return date;    
    }

    private int randBetween(int start, int end) {
        return start + (int)Math.round(Math.random() * (end - start));
    }
Vinay
  • 648
  • 4
  • 12
0
  1. Create a new random number % 28 +1 (for date...just to be safe ... checking 29,30,31 days takes some code).
  2. create another random % 12 +1 (months)
  3. Create a random number %113 + 1900 (for year.. so your min number will be 1900, max will be 2013)
  4. String s= num1+"/"+num2+"/"+num3.
  5. Compile and run.
TheLostMind
  • 35,966
  • 12
  • 68
  • 104