练习计算出一个人已经出生了多少天

请使用日期时间相关的API,计算出一个人已经出生了多少天。

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

 

 

 

posted @ 2022-07-04 14:23  魔光领域  阅读(50)  评论(0编辑  收藏  举报