public static long getRandomTimestamp() {
LocalDate now = LocalDate.now(); // 当前日期
int year = now.getYear(); // 年份
int dayOfYear = now.getDayOfYear(); // 当年的第几天
// 得到今年的1月1日0点0分0秒的时间戳
LocalDateTime thisYearFirstDay = LocalDateTime.of(year, 1, 1, 0, 0, 0);
long start = thisYearFirstDay.toEpochSecond(ZoneOffset.of("+8")) * 1000; // 转换成毫秒
// 得到今年最后一天的23点59分59秒的时间戳
LocalDateTime thisYearLastDay = LocalDateTime.of(year, 12, 31, 23, 59, 59);
long end = thisYearLastDay.toEpochSecond(ZoneOffset.of("+8")) * 1000; // 转换成毫秒
// 随机生成一个在今年内的时间戳
return ThreadLocalRandom.current().nextLong(start, end + 1);
}
java获取随机时间戳
最新推荐文章于 2024-07-13 03:09:42 发布