package com.bjpowernode.hibernate;
import java.text.SimpleDateFormat;
import java.util.Date;
import junit.framework.TestCase;
public class RondomDateTest extends TestCase{
public void testRondomDate() {
Date date = randomDate("2009-07-01","2009-09-01");
System.out.println(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(date));
}
/**
* 获取随机日期
* @param beginDate 起始日期,格式为:yyyy-MM-dd
* @param endDate 结束日期,格式为:yyyy-MM-dd
* @return
*/
private static Date randomDate(String beginDate,String endDate){
try {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
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;
}
}
在某段时间内,随机产生一个时间,包括时分秒
最新推荐文章于 2024-07-13 03:09:42 发布