(练习) 使用日期时间相关的API,计算出一个人已经出生了多少天。
生活随笔
收集整理的這篇文章主要介紹了
(练习) 使用日期时间相关的API,计算出一个人已经出生了多少天。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;public class Test01 {public static void main(String[] args) throws ParseException {System.out.println("請輸入出生日期(年.月.日):");//1.獲取鍵盤輸入的出生日期String birthString = new Scanner(System.in).next();//2.將字符串轉換為日期SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");Date birthDate = sdf.parse(birthString);//3.獲取今天的日期Date todayDate = new Date();//4.將出生日期與今天的日期轉換為毫秒對象long birthSecond = birthDate.getTime();long todaySecond = todayDate.getTime();long resultSecond = todaySecond - birthSecond;if(resultSecond<0){System.out.println("你還沒出生呢!");}else{System.out.println("你已經出生了"+resultSecond/1000/60/60/24+"天");}}
}
總結
以上是生活随笔為你收集整理的(练习) 使用日期时间相关的API,计算出一个人已经出生了多少天。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (Java常用类)日期时间类
- 下一篇: (Java)ArrayList集合