BirthdayCheck

 1 package com.project;
 2
 3 import java.text.SimpleDateFormat;
 4 import java.util.Calendar;
 5 import java.util.Date;
 6 import java.util.Scanner;
 7
 8 public class Birthday {
 9
10     public static void main(String[] args) throws Exception {
11         // TODO Auto-generated method stub
12
13         Scanner input = new Scanner(System.in);
14         System.out.println("请输入你的出生日期:(例:1999-9-9)");
15         String birthday = input.next();
16         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
17         Calendar cal = Calendar.getInstance();
18         Date mybirthday = formatter.parse(birthday);
19         cal.setTime(new Date());
20         int yearNow = cal.get(Calendar.YEAR);// 年
21         int monthNow = cal.get(Calendar.MONTH);
22         int dayNow = cal.get(Calendar.DAY_OF_MONTH);
23         cal.setTime(formatter.parse(birthday));
24         int birthYear = cal.get(Calendar.YEAR);
25         int birthMonth = cal.get(Calendar.MONTH);
26         int birthDay = cal.get(Calendar.DAY_OF_MONTH);
27         int num = 0;
28         if(monthNow == birthMonth&&dayNow == birthDay){
29             System.out.println("祝你生日快乐!!");
30         }
31
32             while(birthYear < yearNow){
33                     cal.set(Calendar.YEAR,cal.get(Calendar.YEAR)+1);
34                     birthYear=cal.get(Calendar.YEAR);
35                     num++;
36             }
37         Date ed=new Date();
38         Date sd=cal.getTime();
39         int days=0;
40             if(((sd.getTime() - ed.getTime()) / (3600 * 24 * 1000))> 0 ){
41                 days=(int)((sd.getTime() - ed.getTime()) / (3600 * 24 * 1000));
42                 System.out.println("距离你" +num+ "周岁生日还有" + days + "天");
43             }else{
44                 cal.set(Calendar.YEAR,cal.get(Calendar.YEAR)+1);
45                 sd=cal.getTime();
46                 days=(int)((sd.getTime() - ed.getTime()) / (3600 * 24 * 1000));
47                 num +=1;
48                 System.out.println("距离你" +num+ "周岁生日还有" + days + "天");
49
50           }
51         long second1 = new Date().getTime();
52         long second2 = mybirthday.getTime();
53         int day1 = (int)((second1 - second2)/(1000*60*60*24));
54         System.out.println("你在这个世界上已经活了" +day1+ "天");
55
56         input.close();
57     }
58
59 }

时间: 2024-10-02 22:46:19

BirthdayCheck的相关文章