使用代码计算你出生到目前过了多少天,用jdk7和jdk8两种方法实现
public class Birthday1 {
public static void main(String[] args) throws ParseException {
//使用代码计算你出生到目前过了多少天,用jdk7和jdk8两种方法实现
//使用jdk7实现
//定义自己的出生年月日
String birthday = "2000年3月3日";
SimpleDateFormat sdt = new SimpleDateFormat("yyyy年MM月dd日");
Date p1 = sdt.parse(birthday);
//获取出生时间的毫秒值
long birthdayTime = p1.getTime();
//获取当前时间的毫秒值
long nowTime = System.currentTimeMillis();
//得到相差天数
long result = (nowTime-birthdayTime)/1000/60/60/24;
System.out.println(result);
//使用jdk8实现
LocalDate birthdayTime2 = LocalDate.of(2000, 3, 3);
LocalDate nowTime2 = LocalDate.now();
long result2 = ChronoUnit.DAYS.between(birthdayTime2,nowTime2);
System.out.println(result2);
}
}
标签:出生,jdk7,代码,System,long,jdk8,LocalDate
From: https://blog.csdn.net/qq_64406993/article/details/136618071