Generate random date of birth

2019-01-04 12:21发布

I'm trying to generate a random date of birth for people in my database using a Java program. How would I do this?

标签: java random
12条回答
Evening l夕情丶
2楼-- · 2019-01-04 13:03

I think this will do the trick:

public static void main(String[] args) {
    Date now = new Date();
    long sixMonthsAgo = (now.getTime() - 15552000000l);
    long today = now.getTime();

    for(int i=0; i<10; i++) {
        long ms = ThreadLocalRandom.current().nextLong(sixMonthsAgo, today);

        Date date = new Date(ms);

        System.out.println(date.toString());
    }

}
查看更多
对你真心纯属浪费
3楼-- · 2019-01-04 13:04

Generating random Date of Births:

import java.util.Calendar;

public class Main {
  public static void main(String[] args) {
    for (int i = 0; i < 100; i++) {
        System.out.println(randomDOB());
    }
  }

  public static String randomDOB() {

    int yyyy = random(1900, 2013);
    int mm = random(1, 12);
    int dd = 0; // will set it later depending on year and month

    switch(mm) {
      case 2:
        if (isLeapYear(yyyy)) {
          dd = random(1, 29);
        } else {
          dd = random(1, 28);
        }
        break;

      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 10:
      case 12:
        dd = random(1, 31);
        break;

      default:
        dd = random(1, 30);
      break;
    }

    String year = Integer.toString(yyyy);
    String month = Integer.toString(mm);
    String day = Integer.toString(dd);

    if (mm < 10) {
        month = "0" + mm;
    }

    if (dd < 10) {
        day = "0" + dd;
    }

    return day + '/' + month + '/' + year;
  }

  public static int random(int lowerBound, int upperBound) {
    return (lowerBound + (int) Math.round(Math.random()
            * (upperBound - lowerBound)));
  }

  public static boolean isLeapYear(int year) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.YEAR, year);
    int noOfDays = calendar.getActualMaximum(Calendar.DAY_OF_YEAR);

    if (noOfDays > 365) {
        return true;
    }

    return false;
  }
}
查看更多
在下西门庆
4楼-- · 2019-01-04 13:07
import java.util.GregorianCalendar;

public class RandomDateOfBirth {

    public static void main(String[] args) {

        GregorianCalendar gc = new GregorianCalendar();

        int year = randBetween(1900, 2010);

        gc.set(gc.YEAR, year);

        int dayOfYear = randBetween(1, gc.getActualMaximum(gc.DAY_OF_YEAR));

        gc.set(gc.DAY_OF_YEAR, dayOfYear);

        System.out.println(gc.get(gc.YEAR) + "-" + (gc.get(gc.MONTH) + 1) + "-" + gc.get(gc.DAY_OF_MONTH));

    }

    public static int randBetween(int start, int end) {
        return start + (int)Math.round(Math.random() * (end - start));
    }
}
查看更多
Root(大扎)
5楼-- · 2019-01-04 13:13

You can checkout randomizer for random data generation.This library helps to create random data from given Model class.Checkout below example code.

public class Person {

    @DateValue( from = "01 Jan 1990",to = "31 Dec 2002" , customFormat = "dd MMM yyyy")
    String dateOfBirth;

}

//Generate random 100 Person(Model Class) object 
Generator<Person> generator = new Generator<>(Person.class);  
List<Person> persons = generator.generate(100);                          

As there are many built in data generator is accessible using annotation,You also can build custom data generator.I suggest you to go through documentation provided on library page.

查看更多
趁早两清
6楼-- · 2019-01-04 13:14

You need to define a random date, right?

A simple way of doing that is to generate a new Date object, using a long (time in milliseconds since 1st January, 1970) and substract a random long:

new Date(Math.abs(System.currentTimeMillis() - RandomUtils.nextLong()));

(RandomUtils is taken from Apache Commons Lang).

Of course, this is far to be a real random date (for example you will not get date before 1970), but I think it will be enough for your needs.

Otherwise, you can create your own date by using Calendar class:

int year = // generate a year between 1900 and 2010;
int dayOfYear = // generate a number between 1 and 365 (or 366 if you need to handle leap year);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, randomYear);
calendar.set(Calendar.DAY_OF_YEAR, dayOfYear);
Date randomDoB = calendar.getTime();
查看更多
forever°为你锁心
7楼-- · 2019-01-04 13:16

For Java8 -> Assumming the data of birth must be before current day:

import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Period;
import java.time.temporal.ChronoUnit;
import java.util.Random;

public class RandomDate {

    public static LocalDate randomBirthday() {
        return LocalDate.now().minus(Period.ofDays((new Random().nextInt(365 * 70))));
    }

    public static void main(String[] args) {
        System.out.println("randomDate: " + randomBirthday());
    }
}
查看更多
登录 后发表回答