Java計算一年中的具體天數

一、不使用各種花裏胡哨

package net.dc.algchallenge;

import java.util.Scanner;
import java.util.Arrays;

public class JudgeDays {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int[] SPECIFICMONTH$1 = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//爲平年時,每月的天數
        final int[] SPECIFICMONTH$2 = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};//爲閏年時,每月的天數
        final Integer[] MONTH31 = {1, 3, 5, 7, 8, 10, 12};//天數爲31的各月
        final Integer[] MONTH30 = {4, 6, 9, 11};//天數爲30的各月
        while (true) {
            //聲明部分:
            boolean isLeapYear, isMatch = false;
            int specificDays = 0;//具體爲一年中多少天

            //輸入部分:
            System.out.print("year = ");
            int year = sc.nextInt();
            System.out.print("month = ");
            int month = sc.nextInt();
            System.out.print("day = ");
            int day = sc.nextInt();

            //判斷是否爲閏年:
            isLeapYear = (year % 4 == 0 && !(year % 100 == 0)) || (year % 400 == 0) || (year % 3200 == 0 && year % 172800 == 0);

            //判斷輸入的月份及其天數是否匹配:
            if (Arrays.asList(MONTH31).contains(month) && (day >= 1 && day <= 31)) {
                isMatch = true;
            } else if (Arrays.asList(MONTH30).contains(month) && (day >= 1 && day <= 30)) {
                isMatch = true;
            } else if (!isLeapYear && month == 2 && (day >= 1 && day <= 28)) {
                isMatch = true;
            } else if (isLeapYear && month == 2 && (day >= 1 && day <= 29)) {
                isMatch = true;
            }

            //輸出部分:
            if (isMatch) {
                if (!isLeapYear) {
                    for (int i = 0; i < month - 1; i++) {
                        specificDays += SPECIFICMONTH$1[i];
                    }
                    specificDays += day;
                    System.out.println(String.format("該日期爲其年的第%d天", specificDays));
                } else {
                    for (int i = 0; i < month - 1; i++) {
                        specificDays += SPECIFICMONTH$2[i];
                    }
                    specificDays += day;
                    System.out.println(String.format("該日期爲其年的第%d天", specificDays));
                }
            } else {
                System.out.println("\033[31m請輸入正確的日期!\033[0m");
            }
        }
    }
}

實現效果:
在這裏插入圖片描述

二、簡易版本

package net.dc.lesson20.exercise;

import java.util.Calendar;
import java.util.Scanner;

public class GetDays {
    public static void main(String[] args) {
        //獲取Calendar實例並實現輸入
        Calendar calendar = Calendar.getInstance();
        Scanner sc = new Scanner(System.in);

        //輸入年月日
        System.out.print("year = ");
        int year = sc.nextInt();
        System.out.print("month = ");
        int month = sc.nextInt();
        System.out.print("day = ");
        int day = sc.nextInt();
        if (month >= 1 && month <= 12) {
            //定義currentDays變量記錄當前天數
            int currentDays = 0;
            for (int i = 1; i < month; i++) {
                //把當前月份的天數設置爲0,就可以得到上個月的完整天數
                calendar.set(year, i, 0);
                currentDays += calendar.get(Calendar.DATE);
            }
            //最後加上本月的天數
            currentDays += day;
            System.out.println(year + "年" + month + "月" + day + "日是該年的第" + currentDays + "天");
        } else {
            System.out.println("\033[31m請輸入正確的時間!\033[0m");
        }
    }
}

實現效果:
在這裏插入圖片描述

三、運用各種日期時間類的方法

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章