需求场景:在数据库造数据的时候需要获取某个时间日期到当前时间之间的随机时间
import java.time.LocalDateTime;
private static LocalDateTime randomTime(){
Random random = new Random();
//2023-1-1 8点到18点之间
LocalDateTime start = LocalDateTime.of(2023, 1, 1, random.nextInt(10)+8, random.nextInt(59), random.nextInt(59));
//这里也可以指定截止的时间,如上开始的格式
LocalDateTime end = LocalDateTime.now();
// 随机生成日期2023-1-1到当前日期之间的日期
LocalDateTime randomDate = start.plusDays(random.nextInt(end.getDayOfYear()));
System.out.println(randomDate.toString());
return randomDate;
}