创建一个从1995.1.1 00:00:00 到 1995.12.31 23:59:59 之间的随机日期
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
//创建一个从1995.1.1 00:00:00 到 1995.12.31 23:59:59 之间的随机日期
public class Randomtime {
public static void main(String[] args) throws ParseException {
String startStr = "1995.1.1 00:00:00";
String endStr = "1995.12.31 23:59:59";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
Date start = sdf.parse(startStr);
Date end = sdf.parse(endStr);
long random = (long) (Math.random()*(end.getTime()-start.getTime()+start.getTime()));
System.out.println(new Date(random));
}
}
本文介绍了一种使用Java编程语言生成指定范围内随机日期的方法。通过设定起始日期为1995年1月1日00:00:00,结束日期为1995年12月31日23:59:59,利用SimpleDateFormat和Math.random()函数,实现了在这一区间内随机选取日期的功能。
5651

被折叠的 条评论
为什么被折叠?



