随机日期工具类
示例代码:
import java.security.SecureRandom;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 随机日期工具类
*/
public final class RandomDateUtil {
public static final ThreadLocal<SimpleDateFormat> DATE_TIME_TL_FORMAT =
ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
/**
* 在[当前时间-seconds, 当前时间+seconds]之间随机生成一个日期时间字符串
*
* @param seconds
* @return 2022-06-09 15:04:08
*/
public static String getCurrentBetweenTimeStr(Integer seconds) {
long midden = System.currentTimeMillis();
long start = midden - 1000 * seconds;
long end = midden + 1000 * seconds;
//调用方法产生随机数
long randomDate = nextLong(start, end);
//格式化输出日期
String format = String.format("%1$tF %1$tT", randomDate);
return format;
}
/**
* 在[当前时间-seconds, 当前时间]之间随机生成一个日期时间字符串
*
* @param seconds
* @return 2022-06-09 15:04:08
*/
public static String getCurrentBeforeTimeStr(Integer seconds) {
long end = System.currentTimeMillis();
long start = end - 1000 * seconds;
//调用方法产生随机数
long randomDate = nextLong(start, end);
//格式化输出日期
String format = String.format("%1$tF %1$tT", randomDate);
return format;
}
/**
* 在[当前时间-seconds, 当前时间+seconds]之间随机生成一个时间戳
*
* @param seconds
* @return
*/
public static long getCurrentBetweenTimestamp(Integer seconds) throws ParseException {
String timeStr = getCurrentBetweenTimeStr(seconds);
long time = DATE_TIME_TL_FORMAT.get().parse(timeStr).getTime();
DATE_TIME_TL_FORMAT.remove();
return time;
}
/**
* 在[当前时间-seconds, 当前时间]之间随机生成一个时间戳
*
* @param seconds
* @return
*/
public static long getCurrentBeforeTimestamp(Integer seconds) throws ParseException {
String timeStr = getCurrentBeforeTimeStr(seconds);
long time = DATE_TIME_TL_FORMAT.get().parse(timeStr).getTime();
DATE_TIME_TL_FORMAT.remove();
return time;
}
/**
* 获取指定时间段的随机时间, 格式: yyyy-MM-dd HH:mm:ss
*
* @param startTime yyyy-MM-dd HH:mm:ss
* @param endTime yyyy-MM-dd HH:mm:ss
* @return 2022-06-09 15:04:08
*/
public static String getDateTimeStr(String startTime, String endTime) {
try {
SimpleDateFormat sdf = DATE_TIME_TL_FORMAT.get();
//指定开始日期
long start = sdf.parse(startTime).getTime();
//指定结束日期
long end = sdf.parse(endTime).getTime();
DATE_TIME_TL_FORMAT.remove();
//调用方法产生随机数
long randomDate = nextLong(start, end);
//格式化输出日期
String format = String.format("%1$tF %1$tT", randomDate);
return format;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 指定年份范围生成随机的日期(yyyy-MM-dd)
*
* @param startYear 开始年份, 如2016
* @param endYear 结束年份, 如2018
* @return
*/
public static Date randomHireday(int startYear, int endYear) {
//指定随机日期开始年份
long start = Timestamp.valueOf(startYear + 1 + "-1-1 0:0:0").getTime();
//指定随机日期开始年份(含)
long end = Timestamp.valueOf(endYear + "-1-1 0:0:0").getTime();
//获得了符合条件的13位毫秒数
long ms = (long) ((end - start) * Math.random() + start);
Date hireday = new Date(ms);
return hireday;
}
private static long nextLong(long start, long end) {
SecureRandom random = new SecureRandom();
return start + (long) (random.nextDouble() * (end - start + 1));
}
public static void main(String[] args) {
System.out.println(getCurrentBetweenTimeStr(10));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
for (int i = 0; i < 10; i++) {
System.out.println(sdf.format(randomHireday(2001, 2015))); //开始年和结束年之间间隔3年以上,如2016~2018是最小的范围
}
}
}