只用于记载工作学习到的知识
1.随机日期
/**
* 获取随机日期
* @param beginDate 起始日期
* @param endDate 结束日期
* @return
*/
public static Date randomDate(String beginDate,String endDate){
try {
SimpleDateFormat format = new SimpleDateFormat(DateUtil.STANDARD_DATETIME_PATTERN);
Date start = format.parse(beginDate);
Date end = format.parse(endDate);
if(start.getTime() >= end.getTime()){
return null;
}
long date = random(start.getTime(),end.getTime());
return new Date(date);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static long random(long begin,long end){
long rtn = begin + (long)(Math.random() * (end - begin));
if(rtn == begin || rtn == end){
return random(begin,end);
}
return rtn;
}
2.mysql 随机查询数据
SELECT * FROM tb_paddling_name as t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM tb_paddling_name)) as id) as t2 WHERE t1.id >= t2.id ORDER BY t1.id asc LIMIT 1;
3.int 值 两数之间随机案例
//年龄区间 强制转换总是向下取值 所以 最大+1 - 最小值 被 1以下随机数相乘 + 最小数
Integer age = (int) Math.random() * (userDto.getAgeBig() + 1 - userDto.getAgeSmall()) + userDto.getAgeSmall();