java - 关于带有日期和随机数的程序的问题
问题描述
我正在尝试创建一个生成随机数的程序。当生成的随机数与当前日期匹配时,它会打印一个计数器来记录它尝试了多少次。日期格式为 ddMMyyyy,生成的随机数为 8 位。最后,它会打印五次日期和计数。这是我到目前为止所拥有的,但它永远不会返回输出。
import java.util.Random;
import java.util.Date;
import java.text.*;
public class Program1 {
public static void main(String[] args) {
Date date = new Date();
Random value = new Random();
int x = value.nextInt();
int counter = 0;
do {
counter++;
} while(!date.equals(value));
if (date.equals(value)){
for(int i = 1; i < 6; i++) {
SimpleDateFormat sdf = new SimpleDateFormat ("ddMMyyyy");
System.out.println("The date is " + sdf.format(date) + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
}
}
}
解决方案
获取long
0 到从纪元开始的毫秒数范围内的数字,并应用格式化程序以获取所需格式的日期字符串。然后将其与今天的日期字符串进行比较。
演示:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
Date now = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
String strToday = sdf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
Date date = new Date(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES));
String strDate = sdf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
示例运行:
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
The date is 21032021!
The Count is: 33263
请注意,java.util
日期时间 API 及其格式化 APISimpleDateFormat
已过时且容易出错。建议完全停止使用它们并切换到现代日期时间 API。*
使用现代日期时间 API:
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
// Change the ZoneId as applicable e.g. ZoneId.of("Europe/London")
ZoneId zoneId = ZoneId.systemDefault();
ZonedDateTime now = ZonedDateTime.now(zoneId);
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("ddMMuuuu", Locale.ENGLISH);
String strToday = dtf.format(now);
int counter = 0;
while (true) {
// Instantiate Date with minutes of 8 digits
LocalDate date = Instant
.ofEpochMilli(TimeUnit.MILLISECONDS
.convert(ThreadLocalRandom.current().nextLong(10000000, 100000000), TimeUnit.MINUTES))
.atZone(zoneId).toLocalDate();
String strDate = dtf.format(date);
if (Objects.equals(strToday, strDate)) {
for (int i = 1; i < 6; i++) {
System.out.println("The date is " + strDate + "!");
System.out.println("The Count is: " + counter);
System.out.println();
}
break;
} else {
counter++;
}
}
}
}
示例运行:
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
The date is 21032021!
The Count is: 9097
* 出于任何原因,如果您必须坚持使用 Java 6 或 Java 7,则可以使用ThreeTen-Backport,它将大部分java.time功能向后移植到 Java 6 和 7。如果您正在为 Android 项目和 Android API 工作level 仍然不符合 Java-8,请检查Java 8+ APIs available through desugaring和How to use ThreeTenABP in Android Project。
推荐阅读
- html - 让内容流出
而不是为它创建滚动条 - sql - 计算导致某个数字的行数
- .net - 有没有办法在特定类型的堆上收集对象的引用?
- javascript - 以 dd/mm/yyyy HH:mm:ss 格式按日期对对象数组进行排序
- sql - Row_Number 上的 Sql 更新
- android - 可以知道我们的应用程序上提供的用户评分吗?使用 Google 提供的 InAppReview 库
- javascript - 从 wordpress 站点调用 php 函数
- python - 我没有收到我想要的数组,有人可以帮我吗?
- javascript - 从 JS 到 php 数组的结果
- sql - 函数使用 dateadd 返回状态 pl sql