title: Java - Generate Random DOB
body: <p>I want to generate random DOB for the given range of year. So I tried the below code</p>
  
  <pre><code>private static 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 static int randBetween(int start, int end) {
          // TODO Auto-generated method stub
           return start + (int)Math.round(Math.random() * (end - start));
      }
  </code></pre>
  
  <p>Main:-</p>
  
  <pre><code>public static void main(String[] args) {
  String dob = randomDataOfBirth(1899, 1937);
  System.out.println(dob);
  }
  </code></pre>
  
  <p>I can be able to generate the random DOB. But only for the Month and Day, I want to add a prefix <code>'0'</code> for the range from 1 to 9
  <br></p>
  
  <pre><code>Month - 01, 02, 03 and ... up to 09
  Day - 01, 02, 03 and ... up to 09
  </code></pre>
  
  <p><br></p>