产生随机的出生日期


68

我正在尝试使用Java程序为数据库中的人们生成一个随机的出生日期。我该怎么做?


3
请不要使用谜语首字母缩写词;)
Danubian Sailor

Answers:


80
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));
    }
}

3
这不是统一的分布,因为例如在2月应该会有更少的人。
lbalazscs

2
@lbalazscs-的确如此。我更新了示例,现在应该更好了。
索尔

6
为了完整性,您应该通过Calendar类(Calendar.YEARCalendar.DAY_OF_YEAR)而不是通过实例使用访问常量gc
mareckmareck'3

1
这个答案很旧,但是月份从0开始计数。因此1月的结果为0。要打印实际的日期,您必须打印(gc.get(gc.MONTH)+1)
Maze

1
@迷宫-即使几年过去了,但一些答案仍然有用且有意义,但是您的观察是完全准确的。解决它。
Saul 2015年

40

java.util.Date有一个自大纪元以来接受毫秒的构造函数,并且java.util.Random一个可以给您随机数毫秒的方法。您将要根据所需的DOB的范围设置随机值的范围,但是应该这样做。

非常粗略:

Random  rnd;
Date    dt;
long    ms;

// Get a new random instance, seeded from the clock
rnd = new Random();

// Get an Epoch value roughly between 1940 and 2010
// -946771200000L = January 1, 1940
// Add up to 70 years to it (using modulus on the next long)
ms = -946771200000L + (Math.abs(rnd.nextLong()) % (70L * 365 * 24 * 60 * 60 * 1000));

// Construct a date
dt = new Date(ms);

1
这正是我初读该问题时开始思考的内容。
AJMansfield

1
这个答案也更符合我的喜好。当一个数字可以确定时,无需分别针对月,日和年进行随机调用。
ewall

1
这是一个很好且简单的解决方案,但我宁愿使用RandomUtils.nextLong(0, 70L * 365 * 24 * 60 * 60 * 1000);apache lang3
Alissa

39

基于Java 8的解决方案的代码段:

Random random = new Random();
int minDay = (int) LocalDate.of(1900, 1, 1).toEpochDay();
int maxDay = (int) LocalDate.of(2015, 1, 1).toEpochDay();
long randomDay = minDay + random.nextInt(maxDay - minDay);

LocalDate randomBirthDate = LocalDate.ofEpochDay(randomDay);

System.out.println(randomBirthDate);

注意:这会生成一个介于1Jan1900(含)和1Jan2015(含)之间的随机日期。

注意:它基于纪元天,即相对于1Jan1970(EPOCH)的天-EPOCH之后为正,EPOCH之前为负


您还可以创建一个小的实用程序类:

public class RandomDate {
    private final LocalDate minDate;
    private final LocalDate maxDate;
    private final Random random;

    public RandomDate(LocalDate minDate, LocalDate maxDate) {
        this.minDate = minDate;
        this.maxDate = maxDate;
        this.random = new Random();
    }

    public LocalDate nextDate() {
        int minDay = (int) minDate.toEpochDay();
        int maxDay = (int) maxDate.toEpochDay();
        long randomDay = minDay + random.nextInt(maxDay - minDay);
        return LocalDate.ofEpochDay(randomDay);
    }

    @Override
    public String toString() {
        return "RandomDate{" +
                "maxDate=" + maxDate +
                ", minDate=" + minDate +
                '}';
    }
}

并像这样使用它:

RandomDate rd = new RandomDate(LocalDate.of(1900, 1, 1), LocalDate.of(2010, 1, 1));
System.out.println(rd.nextDate());
System.out.println(rd.nextDate()); // birthdays ad infinitum

4
这是一个很好的答案,但是通过对构造函数中的min和max进行toEpochDay()转换并保存int结果而不是LocalDates,可以使实用程序类的效率更高。然后,只需要执行一次,而不是每次调用nextDate()一次。
L. Blanc

您可以int通过调用ThreadLocalRandom.current()。nextLong(n)来避免将时代推后
jaco0646 '18

这将进入我的测试工具箱中以生成模拟报告。感谢您的贡献
Eddie B

21

您需要定义一个随机日期,对吗?

一种简单的方法是Date使用long自1970年1月1日以来的时间以毫秒为单位)生成一个新对象,然后减去一个随机数long

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

RandomUtils取自Apache Commons Lang)。

当然,这远不是真正的随机日期(例如,您不会在1970年之前获得日期),但我认为这足以满足您的需求。

否则,您可以使用Calendar类创建自己的日期:

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();

5
仅使用标准随机数真的那么麻烦吗?
AJMansfield

1
这个答案有点过时了。当我输入时new Date(Math.abs(System.currentTimeMillis() - RandomUtils.nextLong()));,它表明该方法已被弃用。但是,我在其他任何地方都找不到并行的等效方法。
Yokhen 2014年

8

对于Java8->假设出生数据必须在当前日期之前:

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());
    }
}

1
考虑每次使用一个ThreadLocalRandom而不是实例化一个新的Random
罗勒·布尔克

真正。更好的做法。
Witold Kaczurba,

3

如果您不介意在代码中添加新库,则可以使用MockNeat(免责声明:我是其中之一)。

MockNeat mock = MockNeat.threadLocal();

// Generates a random date between [1970-1-1, NOW)
LocalDate localDate = mock.localDates().val();
System.out.println(localDate);

// Generates a random date in the past
// but beore 1987-1-30
LocalDate min = LocalDate.of(1987, 1, 30);
LocalDate past = mock.localDates().past(min).val();
System.out.println(past);

LocalDate max = LocalDate.of(2020, 1, 1);
LocalDate future = mock.localDates().future(max).val();
System.out.println(future);

// Generates a random date between 1989-1-1 and 1993-1-1
LocalDate start = LocalDate.of(1989, 1, 1);
LocalDate stop = LocalDate.of(1993, 1, 1);
LocalDate between = mock.localDates().between(start, stop).val();
System.out.println(between);

2

随机产生出生日期:

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;
  }
}

2

您可以检出randomizer以生成随机数据。此库可帮助根据给定的Model类创建随机数据。

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);                          

由于使用注释可以访问许多内置数据生成器,因此您也可以构建自定义数据生成器。我建议您阅读库页面上提供的文档。


2

看这个方法:

public static Date dateRandom(int initialYear, int lastYear) {
    if (initialYear > lastYear) {
        int year = lastYear;
        lastYear = initialYear;
        initialYear = year;
    }

    Calendar cInitialYear = Calendar.getInstance();
    cInitialYear.set(Calendar.YEAR, 2015);
    long offset = cInitialYear.getTimeInMillis();

    Calendar cLastYear = Calendar.getInstance();
    cLastYear.set(Calendar.YEAR, 2016);
    long end = cLastYear.getTimeInMillis();

    long diff = end - offset + 1;
    Timestamp timestamp = new Timestamp(offset + (long) (Math.random() * diff));
    return new Date(timestamp.getTime());
}

1

我认为这可以解决问题:

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());
    }

}

1

如果您不介意第三方库,则Utils库中有一个RandomDateUtils,它会生成随机的java.util.Dates以及Java 8的日期和时间API中的所有日期,时间,瞬间和持续时间。

LocalDate birthDate = RandomDateUtils.randomPastLocalDate();
LocalDate today = LocalDate.now();
LocalDate under18YearsOld = RandomDateUtils.randomLocalDate(today.minus(18, YEARS), today);
LocalDate over18YearsOld = RandomDateUtils.randomLocalDateBefore(today.minus(18, YEARS));

它在Maven中央存储库中,位于:

<dependency>
  <groupId>com.github.rkumsher</groupId>
  <artifactId>utils</artifactId>
  <version>1.3</version>
</dependency>

1

最简单的方法:

public static LocalDate randomDateOfBirth() {
    final int maxAge = 100 * 12 * 31;
    return LocalDate.now().minusDays(new Random().nextInt(maxAge));
}

0

我正在研究Scala并最终使用Googling Java解决方案在范围之间选择随机日期。我发现这篇文章超级有帮助,这是我的最终解决方案。希望它可以帮助将来的Scala和Java程序员。

import java.sql.Timestamp

def date_rand(ts_start_str:String = "2012-01-01 00:00:00", ts_end_str:String = "2015-01-01 00:00:00"): String = {
    val ts_start = Timestamp.valueOf(ts_start_str).getTime()
    val ts_end = Timestamp.valueOf(ts_end_str).getTime()
    val diff = ts_end - ts_start
    println(diff)
    val ts_rand = new Timestamp(ts_start + (Random.nextFloat() * diff).toLong)
    return ts_rand.toString
}                                         //> date_rand: (ts_start_str: String, ts_end_str: String)String

println(date_rand())                      //> 94694400000
                                              //| 2012-10-28 18:21:13.216

println(date_rand("2001-01-01 00:00:00", "2001-01-01 00:00:00"))
                                              //> 0
                                              //| 2001-01-01 00:00:00.0
println(date_rand("2001-01-01 00:00:00", "2010-01-01 00:00:00"))
                                              //> 283996800000
                                              //| 2008-02-16 23:15:48.864                    //> 2013-12-21 08:32:16.384

0
int num = 0;
char[] a={'a','b','c','d','e','f'};
String error = null;
try {
    num = Integer.parseInt(request.getParameter("num"));
    Random r = new Random();
    long currentDate = new Date().getTime();
    ArrayList<Student> list = new ArrayList<>();
    for (int i = 0; i < num; i++) {
        String name = "";
        for (int j = 0; j < 6; j++) {
            name += a[r.nextInt(5)];
        }

        list.add(new Student(i + 1, name, r.nextBoolean(), new Date(Math.abs(r.nextLong() % currentDate))));
    }

    request.setAttribute("list", list);
    request.setAttribute("num", num);
    request.getRequestDispatcher("student.jsp").forward(request, response);
} catch (NumberFormatException e) {
    error = "Please enter interger number";
    request.setAttribute("error", error);
    request.getRequestDispatcher("student.jsp").forward(request, response);
}

在回答一个旧问题时,如果您包含一些上下文来解释您的答案有什么帮助,特别是对于已经接受了该问题的问题,那么对于其他StackOverflow用户来说,您的答案将更加有用。请参阅:如何写一个好的答案
David Buck
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.