日期:2014-05-20  浏览次数:20716 次

怎样在java中随机生成日期,日期的格式最好是yyyy-mm-dd
如题
找了好多资料,还是没有找到想要的结果
请各位帮帮忙
非常感谢

------解决方案--------------------
你要指定个生成范围才行,否则跨度太大了
Random rand = new Random();
SimpleDateFormat format = new SimpleDateFormat( "yyyy-MM-dd ");
Calendar cal = Calendar.getInstance();
cal.set(1900, 0, 1);
long start = cal.getTimeInMillis();
cal.set(2008, 0, 1);
long end = cal.getTimeInMillis();
for(int i = 0; i < 10; i++) {
Date d = new Date(start + (long)(rand.nextDouble() * (end - start)));
System.out.println(format.format(d));
}