你来到人间多少天了?

package test;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.Scanner;

/**

* 你来到人间多少天了

* @author gaofuzhi

*/

public class CalendarDemo {

public static void main(String[] args) {

System.out.println("请按照如下格式“yyyy-MM-dd”输入您的出生年月日:");

Scanner input = new Scanner(System.in);

String brithDate = input.next();

try {

long allDay2 = calculateDay(brithDate);

System.out.println("您已经来人间"+allDay2+"天了");

} catch (ParseException e) {

e.printStackTrace();

}

input.close();

}

public  static long calculateDay(String date) throws ParseException{

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

Date newDate = simpleDateFormat.parse(date);

Calendar calendar = Calendar.getInstance();

long nowTime = calendar.getTimeInMillis();

calendar.setTime(newDate);

long beforeTime = calendar.getTimeInMillis();

long time = nowTime - beforeTime;

long allDay = time/1000/60/60/24;

return allDay;

}

}

你可能感兴趣的:(你来到人间多少天了?)