1.要求:
public static void main(String[] args) {
// 1900+112 1+2 0+12 8-8 0+0
Date date = new Date(Date.UTC(113, Calendar.MARCH,12,-8,0,0));
// 1900+112 0+3 0+12 0+0 0+0 0+0
//Date date=new Date(112, 3, 12, 0, 0, 0); 简写(建议使用)
// System.out.println(date.getDate());
//自定义时间
Scanner scanner = new Scanner(System.in);
String time=scanner.next();
scanner.close();
//处理输入时间
String []b=handle(time);
Date youSet = new Date(Date.UTC((Integer.parseInt(b[0])-1900),Integer.parseInt(b[1]),Integer.parseInt(b[2]),-8,0,0));
long l = (youSet.getTime() - date.getTime())/(24*60*60*1000); //计算差距ms
System.out.println(l);
}
public static String[] handle(String time){
String []a=time.split("\\.");
// System.out.println(Arrays.toString(a));
return a;
}
2.要求: 生成某个时间范围的随机一天
//借助随机数,创建一个从1995.1.1 00:00:00 到 1995.12.31 23:59:59 之间的随机日期
long start = (new Date(95, 1, 1, 0, 0, 0)).getTime();
long end = (new Date(95, 12, 31, 23, 59, 59)).getTime();
long randoms = (long) (Math.random() * (end - start + 1) + start);
System.out.println(new Date(randoms)); //可以通过毫秒创建date对象
Date计算时间差,指定时间