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");
        }
    }
}

实现效果:
在这里插入图片描述

三、运用各种日期时间类的方法

猜你喜欢

转载自blog.csdn.net/yeyu_xing/article/details/106632561