任务:生成指定范围的随机日期对象2000-01-01 00:00:00-now
public class RandomDate {
public static void main(String[] args) throws ParseException {
//获取当前时间的日期
Calendar calendar = Calendar.getInstance();
long now = calendar.getTimeInMillis();
//设置指定的开始日期
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = sdf.parse("2000-01-01 00:00:00");
long before = date.getTime();
Random random = new Random();
//产生long类型指定范围随机数日期对象
long randomDate = before + (long) (random.nextFloat() * (now - before + 1));
//格式化输出随机日期对象
System.out.printf("从2000-01-01 00:00:00到现在的一个随机日期为:%1$tF %1$tT", randomDate);
}
}