计算用户输入的日期离1900年1月1日相距多少天

public static void test9() {
    Scanner sc = new Scanner(System.in);
    int year = sc.nextInt();
    int month = sc.nextInt();
    int day = sc.nextInt();
    int sumDay = day - 1;

    //年相差 闰年 366 和平年365
    for (int i = 1900; i < year; i++) {
        if (((i % 4 == 0) && (i % 100 != 0)) || i % 400 == 0) {
            sumDay += 366;
        } else {
            sumDay = sumDay + 365;
        }
    }
    for (int i = 1; i < month; i++) {
        switch (i) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                sumDay += 31;
                break;
            case 2:
                if (((year % 4 == 0) && (year % 100 != 0)) || year % 400 == 0) {
                    sumDay += 29;
                } else {
                    sumDay = sumDay + 28;
                }
                break;
            default:
                sumDay += 30;
        }
    }
    System.out.println("相差天数:" + sumDay);
}

你可能感兴趣的:(第一阶段,java)