计算出生天数
分析:
1.需要获取出生日期
2.需要获取现在的日期
3.把出生日期和现在日期变成毫秒值
4.用现在日期的毫秒值减去出生日期的毫秒值
5.把毫秒值的差值转换为天(s/1000/60/60/24)
package Day01;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class DaysOfLife {
public void days() throws ParseException {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入您的出生日期:(格式:yyyy年MM月dd日)");
String dateOfBirth = scanner.next();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
Date date = simpleDateFormat.parse(dateOfBirth);
long dateOfBirthTime = date.getTime();
Date date1 = new Date();
long nowTime = date1.getTime();
long time = nowTime - dateOfBirthTime;
long days = time / 1000 / 60 / 60 / 24;
System.out.println(days);
}
public static void main(String[] args) {
DaysOfLife daysOfLife=new DaysOfLife();
try {
daysOfLife.days();
} catch (ParseException e) {
e.printStackTrace();
}
}
}