【问题标题】:Random generate a date which is after the date in string format [duplicate]以字符串格式随机生成日期之后的日期[重复]
【发布时间】:2016-10-20 13:49:19
【问题描述】:

我有一个关于随机生成日期的问题。所以我想做的是随机生成 1-30 之间的一天,1-12 之间的月份,默认情况下是 2016 年。

Random rand = new Random();
int date = 1 + rand.nextInt((30 - 1) + 1);
int month = 1 + rand.nextInt((12 - 1) + 1);
if(month == 1){
    monthStr = "January";
}else if(month == 2){
    monthStr = "February";
}else if(month == 3){
    monthStr = "March";
}else if(month == 4){
    monthStr = "April";
}else if(month == 5){
    monthStr = "May";
}else if(month == 6){
    monthStr = "June";
}else if(month == 7){
    monthStr = "July";
}else if(month == 8){
    monthStr = "August";
}else if(month == 9){
    monthStr = "September";
}else if(month == 10){
    monthStr = "October";
}else if(month == 11){
    monthStr = "November";
}else if(month == 12){
    monthStr = "December";
}

我使用 for 循环随机生成日期 100 次。然后,我将字符串附加在一起以将其存储到字符串数组列表中。

ArrayList<String> dateTimeList = new ArrayList<String>();
dateTimeList.add(date + " " +  monthStr + " 2016");

之后,我将整个列表传递给另一个方法。在这种方法中,我试图再次生成随机日期,但在这种情况下,我需要生成一个在上述日期之后的日期。

换句话说,我第一次随机生成100个日期是申请日期。然后我将整个列表传递给另一个方法,在该方法中,我需要在每个提交日期之后生成另外 100 个日期。

任何想法我怎么能做到这一点?我知道还有其他方法可以生成随机日期,但我更喜欢这种方式,因为我将在其他地方使用它。

提前致谢。

【问题讨论】:

  • 嗯,因为我会将这些日期插入数据库,并且它以特定格式工作,例如 2016 年 1 月 25 日。这就是我这样做的原因
  • 您可以将日期转换为毫秒,然后将毫秒作为最小值传递给随机函数。您将从随机获得的数字解释为日期
  • @DimaSan 对不起,我认为这不是线程的重复。该线程要求的是在没有任何条件的情况下生成随机日期。但我问的是如何生成某个日期之后的日期。
  • 您的解决方案中几个月内的第 31 天怎么样?那么2月30日呢?使用默认的 DateTime 类可以省去很多麻烦...
  • 好吧,也许你是对的。但是是什么阻止您将此条件添加到工作解决方案中?

标签: java date random


【解决方案1】:

我假设您正在使用带有新 Date API 的最新 Java。

首先创建可接受的最早日期,例如:

LocalDate baseDate = LocalDate.now(); //use today, OR
LocalDate baseDate = LocalDate.of(2016, 11, 30); //use any date - you can randomize the year/month/date if you want

接下来,选择一个随机数。请注意,您可以在日期中添加天数、数月或数年 - 此数字应代表其中之一。让我们假设几天。

Integer maximumRandomValue = 100; //we want to add a number of days between 0-100
Integer randomDays = (int)(maximumRandomValue*Math.random());

现在只需将它们添加到您的基准日期:

LocalDate randomDate = baseDate.plusDays(randomDays);

此外,如果您想将其转换为字符串(尽管您的数据库代码应该能够为您处理)这是一种简单的方法(使用您提供的格式)

String dateAsString = DateTimeFormatter.ofPattern("d MMMM yyyy").format(randomDate);

更多格式选项请参考API docs of DateTimeFormatter

【讨论】:

  • 但是日期的格式是什么?是 2016 年 10 月 20 日吗?
  • LocalDate 对象没有特定的格式——它只是与日期相关的信息。我刚刚添加了一个示例,如何以您想要的方式格式化日期 - 基本上使用 DateTimeFormatter 类。它允许您以任何您想要的方式对其进行格式化。
  • 抱歉,有什么办法可以扭转它,使其以这种方式工作:首先我随机生成 100 个日期,然后我随机生成前 100 个日期之后的另外 100 个日期。原因来自我了解您的代码是您在今天的日期之后随机生成 100 个日期。
  • 当然,只需将第一行替换为:int year = 2015;整数月 = 10;整数天 = 8; LocalDate baseDate = LocalDate.of(年、月、日);当然,您也可以随机化这些年/月/日值。更新了答案以包含此
  • 非常感谢!它有效!
【解决方案2】:

您可以将日期转换为毫秒,然后使用随机函数生成一个大整数。将此随机数添加到毫秒并将其转换回日期。

这是怎么回事=>

import java.math.BigInteger;
import java.util.Date;
import java.util.Random;

    public class generateRandomDate {

        public static void generateRandomDate(){

            Date d = new Date();
            System.out.println("Input Date ==> "+ d);
            System.out.println("Input Milliseconds ==> "+ d.getTime());
            BigInteger randomBigInteger = nextRandomBigInteger(BigInteger.valueOf(d.getTime()));
            System.out.println("RandomBinInteger ==> "+ randomBigInteger);
            System.out.println("Next random Date ==> "+ new Date(d.getTime() + randomBigInteger.longValue()));
        }

        public static BigInteger nextRandomBigInteger(BigInteger n) {
            Random rand = new Random();
            BigInteger result = new BigInteger(n.bitLength(), rand);
            while( result.compareTo(n) >= 0 ) {
                result = new BigInteger(n.bitLength(), rand);
            }
            return result;
        }
        public static void main(String[] args) {
            generateRandomDate();
        }


    }

【讨论】:

  • 嗯,你介意给我举几个例子吗?
  • 用代码 sn-p 更新了答案。您可以使用函数的输入来控制随机大整数的生成。我现在已经将当前日期的长值传递给随机生成器。希望对你有帮助!
猜你喜欢
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 2012-01-07
  • 2022-06-14
  • 1970-01-01
相关资源
最近更新 更多