日期时间练习题二

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

public static void live() throws Exception {
        
        System.out.println("请输入你的生日 YYYY-MM-DD:");        
        String birthdayStr = new Scanner(System.in).next();//birthdayStr
        
        SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthDate = spf.parse(birthdayStr);//生日的 Date日期
        
        long birthTime = birthDate.getTime();//时间戳时间
        long nowTime = new Date().getTime();//时间戳时间
        
        long betweenTime = (nowTime-birthTime);
        long days = betweenTime/1000/60/60/24; //毫秒->秒->分->小时->天
        System.out.println("从出生到现在 你过了"+days+"天");
        
        
    }
    

 

posted @ 2020-05-15 13:55  CherryYang  阅读(104)  评论(0编辑  收藏  举报