//随机生成2023-1-1 0:0:0 到 现在的随机日期10个?
public void randomDay() throws ParseException {
//格式化Date日期
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse("2023-1-1 0:0:0"); //指定日期
long time = date.getTime(); //指定日期的时间戳
long timeMillis = System.currentTimeMillis(); //系统当前时间的时间戳
for (int i = 0; i < 10; i++) {
long random = (long) (Math.random()*(timeMillis-time) + time);
System.out.println(simpleDateFormat.format(random));
}
}
/**
输出结果
2023-02-08 10:09:12
2023-01-13 08:42:30
2023-02-08 09:28:25
2023-01-18 18:46:16
2023-01-30 08:55:43
2023-01-25 23:52:48
2023-01-13 18:32:11
2023-01-23 19:59:23
2023-02-12 06:50:11
2023-01-26 22:24:43
*/
java中指定日期 -- 当前时间之内随机生成日期
最新推荐文章于 2023-07-12 21:35:09 发布