JAVA一周学习笔记之获取生日距今天数

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class demoTest {
    public static void main(String[] args) throws ParseException {
        //1.使用Scanner类中的方法next,获取出生日期
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入您的出生日期:格式为yyyy-MM-dd");
        String birthdayDateString = sc.next();
        //2.使用DateFormat类中的方法parse,把字符串的出生日期解析为Date格式
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Date birthdayDate=sdf.parse(birthdayDateString);
        //3.Date格式的出生日期转换为毫秒值
        long birthdayDateTime=birthdayDate.getTime();
        //4.获取当前的日期,转换为毫秒值
        long todayTime=new Date().getTime();
        //5.使用当前日期的毫秒值-出生日期的毫秒值
        long time=todayTime-birthdayDateTime;
        //6.把毫秒值的差值转换为天(s/1000/60/60/24)
        System.out.println(time/1000/60/60/24);



    }
}

 

posted @ 2020-07-28 15:00  JasonLin233  阅读(339)  评论(0)    收藏  举报