查看当前日期是这一年的第几天


package studying;

import java.util.Scanner;

public class JudgmentDate {
    
    /*
     * Title: Enter a certain day of the year, to determine the day of the year.
     */
    
    public static void main(String[] args) {
        int year;
        int month;
        int day = 0;
        int days;
        //Cumulative days
        int d = 0;
        int e = 0;
        Scanner input = new Scanner(System.in);
        do {
            System.out.println("Please enter year:");
            year = input.nextInt();
            System.out.println("Please enter month:");
            month = input.nextInt();
            System.out.println("Please enter day:");
            days = input.nextInt();
            if(month < 0 || month > 12 || days > 31) {
                System.out.println("Input error!");
                e = 1;
            }
        }while( e == 1);
        
        for(int i = 1; i < month; i++) {
            switch(i) {
            case 1:case 3:case 5:case 7:case 8:case 10:case 12:{
                day = 31;
                break;
            }
            case 4:case 6:case 9:case 11:{
                day = 30;
                break;
            }
            case 2:{
                /*
                 * 闰年:①非整白年数除以4,无余数为闰年,有余数为平年;
                 * ②整百年数除以400,无余数。
                 * 二月:平年28天,闰年29天。
                 */
                if((year % 100 != 0 && year % 400 == 0) ||(year % 100 == 0) && (year % 400 == 0)) {
                    day = 29;
                }else {
                    day = 28;
                }
            }
            default:break;
            }
            d += day;
        }
        System.out.println("This is the " + (d +days) + "day of " + year);
    }

}

 结果截图:

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM