java 输入生日 判断年龄 星期几 距今天数

这是一个Java程序,用户输入日期(格式:yymmdd),程序会判断日期是否有效,然后计算年龄,输出该日期是星期几,并计算距离今天相隔的天数。如果日期在今天之前,还会提示用户尚未出生。
package com.minrisoft;
import java.lang.String; 
import java.util.Scanner;
public class Riqi {
  public static boolean leapYear(int y){
        if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
            return true;
        else
            return false;
    }
  public static void calcWeek(int a,int b,int c){
        if(b == 1){
            b = 13;
            a =a - 1;
            }
        if(b ==12){
            b = 14;
            a = a - 1; 
        }
        int h = (int) ((c + (26 * (b + 1) / 10) + (a % 100) + ((a % 100) / 4) + ((a / 100) / 4) + 5 * (a / 100)) % 7); 
        switch (h) { 
        case 0: 
        System.out.println("星期六"); 
        break; 
        case 1: 
        System.out.println("星期天"); 
        break; 
        case 2: 
        System.out.println("星期一"); 
        break; 
        case 3: 
        System.out.println("星期二"); 
        break; 
        case 4: 
        System.out.println("星期三"); 
        break; 
        case 5: 
        System.out.println("星期四"); 
        break; 
        case 6: 
        System.out.println("星期五"); 
        break; 
       }
    }
  public static int countDay(int y, int m, int d){
        int month_1[] ={ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int month_2[] ={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
        int count = 0;
        do {
            count += 365;
            if (leapYear(y) == true)
                count += 1;
        }while((--y)!= 0);
        do{
            if (leapYear(y) == true)
                count += month_1[m-1];
            else
                count += month_2[m-1];
        }while ((--m)!= 0);
        count += d;
        return count;
}
  public static void main(String [] args){
      Scanner sc = new Scanner(System.in);
        System.out.print("请输入日期(格式:yymmdd):");
        String y_m_d_1 = sc.nextLine();
        sc.close();
        int x =Integer.parseInt(y_m_d_1.substring(0,4));
        int y =Integer.parseInt(y_m_d_1.substring(4,6));
        int z =Integer.parseInt(y_m_d_1.substring(6,8));
        if (y >= 1 && y <= 12 && z >= 1 && z <= 31){
            if (y == 4 || y == 6 || y == 9 || y == 11){
                if (z > 30){
                    System.out.println("输入错误");
                    return;
                }
            }
            if (y == 2){
                if (leapYear(y) == true){
                    if (z > 29){
                        System.out.println("输入错误");
                        return;
                    }
                }
                else{
                    if (z > 28){
                        System.out.println("输入错误");
                        return;
                    }
                }
            }
        }
        else{
            System.out.println("输入错误");
            return;
        } 
        System.out.println(x + "年" + y + "月" + z + "日:输入正确");
    
            int year_1 =  countDay(x,y,z);
            int year_2 =  countDay(2017,04,21);
            calcWeek(x,y,z); 
            if (year_1 == year_2)
                System.out.println("处于今天");
            if (year_1 < year_2)
                System.out.println("距离" + (year_2 - year_1) + "天");
            if (year_1 > year_2){
                System.out.println("距离" + (year_1 - year_2) + "天");
                System.out.println(x + "年" + y + "月" + z + "日:没有出生");
                }
            else{
                    int n = 2017 - x;
                    if (y > 4 || (x != 2017 && y == 4 && z > 21))
                        n -= 1;
                    System.out.println("今年"+ n + "岁");
                }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值