1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| package com.itheima.demo03.DateFormat;
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Scanner;
public class Demo02Test { public static void main(String[] args) throws ParseException { Scanner sc = new Scanner(System.in); System.out.println("请输入您的出生日期,格式:yyyy-MM-dd"); String birthdayDateString = sc.next(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Date birthdayDate = sdf.parse(birthdayDateString); long birthdayDateTime = birthdayDate.getTime(); long todayTime = new Date().getTime(); long time = todayTime-birthdayDateTime; System.out.println(time/1000/60/60/24); } }
|