每月产生随机日期

时间:2019-05-31 15:32:29

标签: java

我有一个在模拟软件中运行的程序,该程序需要每月生成随机日期。

例如:在第一步中,假设生成了1月的第一个随机日期:1月22日。它应该在第二时间步骤中从2月1日开始选择下一个随机日期。

我使用了以下Java代码:

String strDate = dateFormat.format(date());
OrderDate = LocalDate.parse(strDate)
                     .plus(Period.ofDays((1 + new Random().nextInt(30))));

问题是我该如何确保它在第二步中肯定选择下个月的下一个日期。目前,它可以选择一个随机日期为1月5日,第二个日期为1月22日。我想确保它在一个月内只选择一个日期

2 个答案:

答案 0 :(得分:3)

使用plusMonths进入下个月,然后使用withDayOfMonth<section id="allyes"> <div id="allyesLogos"> <h1 id="allyesTitle">alianzas comericales</h1> <div class="logosTable"> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/4yousee.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/benq.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/bright-sign.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/dell.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/dynascan.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/elo.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/fortinet.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/glassapps.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/hisense.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/iadea.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/lenovo.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/LG.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/nec.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/panasonic.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/samsung.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/screengoo.webp'?>" alt="ally"></div> <div class='allyLogo'><img src="<?php echo $httpProtocol.$host.$url.'img/logos-alianzas/sharp.webp'?>" alt="ally"></div> </div> </div> </section>lengthOfMonth之间设置随机的一天:

1

答案 1 :(得分:3)

要生成特定月份的随机日期,您需要知道该月的天数。使用YearMonth类可以工作数月。

例如为2019年的每个月生成随机日期:

Random random = new Random();
for (int month = 1; month <= 12; month++) {
    YearMonth yearMonth = YearMonth.of(2019, month);
    int daysInMonth = yearMonth.lengthOfMonth();
    int dayOfMonth = random.nextInt(daysInMonth) + 1;
    LocalDate localDate = yearMonth.atDay(dayOfMonth);
    System.out.println(localDate);
}

示例输出

2019-01-12
2019-02-24
2019-03-07
2019-04-28
2019-05-28
2019-06-07
2019-07-20
2019-08-03
2019-09-20
2019-10-31
2019-11-12
2019-12-09

如果您将开始日期作为字符串,并且想要下个月的随机日期,则可以这样做:

Random random = new Random();
String strDate = "2019-01-22";

YearMonth nextMonth = YearMonth.from(LocalDate.parse(strDate)).plusMonths(1);
LocalDate orderDate = nextMonth.atDay(random.nextInt(nextMonth.lengthOfMonth()) + 1);

System.out.println(orderDate);

示例输出

2019-02-13
相关问题