java实现产生固定时间段里面的随机日期
public class test{ public static void main(String[] args) { Date randomDate = randomDate("2012-06-01", "2012-12-12"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(randomDate。toString()); } 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 rtnn = begin + (long) (Math.random() * (end - begin)); if (rtnn == begin || rtnn == end) { return random(begin, end); } return rtnn; } }