Java里,如何得到一个月有多少天

如题所述

按照你的要求,如何得到一个月有多少天,这里并没有说是什么年份,所以默认当年(不同年份的月份天数可能不一样,例如闰年的二月)

因此问题变为

输入条件:指定某一个月

输出结果:当年这个月份的天数


思路:在Java8里新的时间API里,月份已经被抽象成了枚举Month,所以可以把输入条件定义为枚举类型的Month,然后获取当前时间X,把时间X的月份修改为输入条件的月份,此时时间X变为X1,根据本身提供的方法lengthOfMonth就可以直接得到X1所在当月的天数了


代码:(请将JDK升到8

public static void main(String[] args) {
    System.out.println(countDaysInMonth(Month.MAY));
}

public static int countDaysInMonth(Month month){
    // 获取当前时间
    LocalDate now = LocalDate.now();
    System.out.println(now);

    // 把当前时间的月份修改为输入的月份
    LocalDate thisMonthDate = now.withMonth(month.getValue());
    System.out.println(thisMonthDate);
    return thisMonthDate.lengthOfMonth();
}

也可以连着写,更美观点

public static int countDaysInMonth(Month month){
    return LocalDate.now()
                    .withMonth(month.getValue())
                    .lengthOfMonth();
}

非常直观且易懂好用,在Java8里with就代表着修改意思,withMonth那就是修改月份,所以整个代码读下来就变成

    获取当前时间A

    修改A的月份为输入条件得到时间B

    计算B所在月的天数


温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-12-09



考虑闰年的计算方式

package com.mdsd.dip.sdk;
import java.util.Scanner;

/**
 * 求用JAVA计算某年某月的天数?
 */
public class DayTest {
    public static void main(String[] args) {
        // 定义年份
        int year = -1;
        // 定义月份
        int month = -1;

        // 使用Scanner
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入年");
        year = scanner.nextInt();
        System.out.println("请输入月");
        month = scanner.nextInt();

        System.out.println(year + "年" + month + "月有" + days(year, month) + "天");

    }

    public static int days(int year, int month) {
        int days = 0;
        if (month != 2) {
            switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                days = 31;
                break;
            case 4:
            case 6:
            case 9:
            case 11:
                days = 30;

            }
        } else {
            // 闰年
            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                days = 29;
            else
                days = 28;

        }
        return days;
    }
}

第2个回答  2016-12-09
思路:java中有没有获取指定月的天数方法不清楚,但是可以自己写一个,首先判断135781012这几个月都是31天,在46911是30天,在判断是否是闰年平年,从而判断2月是28还是29天。
第3个回答  2016-12-09
static final int MONTH_LENGTH[]
    = {31,28,31,30,31,30,31,31,30,31,30,31}; // 0-based
static final int LEAP_MONTH_LENGTH[]
    = {31,29,31,30,31,30,31,31,30,31,30,31}; // 0-based
    
public int monthLength(int month, int year) {
    return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month];
}

思路: 定义两个数组,分别为平年和闰年的月份天数。 然后根据年份和月份判断

第4个回答  2016-12-09
// year 年份(四位数)
// month 月份(从1开始)
public static int getMonthDays(int year, int month) {
    if (month == 2) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            return 29;
        } else {
            return 28;
        }
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    } else {
        return 31;
    }
}