[Hi寻找一个示例代码,该代码将在一定范围内(1960、1970)返回1968-02-03 23:02:03的RandomDateTime。
YYYY-MM-DD HH:MM:SS。
我的代码正在返回我不希望的时区。...
Random rand = new Random();
LocalDateTime minInclusive = LocalDateTime.of(1960, Month.JANUARY, 1, 0, 0);
LocalDateTime maxExclusive = LocalDateTime.of(1971, Month.JANUARY, 1, 0, 0);
int seconds = Math.toIntExact(ChronoUnit.SECONDS.between(minInclusive, maxExclusive));
int choice = rand.nextInt(seconds);
LocalDateTime randomDateTime = minInclusive.plusSeconds(choice);
System.out.println(randomDateTime);
示例输出(从现在运行):
1960-12-05T19:05:28
Link: Oracle tutorial: Date Time解释如何使用java.time。