Java Day in Month daysInMonth(int month, int year)

Here you can find the source of daysInMonth(int month, int year)

Description

return the number of days in the month of the year.

License

Open Source License

Parameter

Parameter Description
month the month (1,2,...,12)
year the year (valid years are 1000-8999)

Return

the number of days in the month.

Declaration

public static int daysInMonth(int month, int year) 

Method Source Code

//package com.java2s;
/*//from  w  w  w  .j  av  a  2  s  . c  o m
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

public class Main {
    private final static int[][] daysInMonth = { { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
            { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } };

    /**
     * return the number of days in the month of the year.  Note the 
     * result is not valid for years less than 1600 or so.  
     * @param month the month (1,2,...,12)
     * @param year the year (valid years are 1000-8999)
     * @return the number of days in the month.
     */
    public static int daysInMonth(int month, int year) {
        return daysInMonth[isLeapYear(year) ? 1 : 0][month];
    }

    /**
     * return the leap year for years 1581-8999.
     * @param year the four-digit year.
     * @return true if the year is a leap year.
     */
    public static boolean isLeapYear(int year) {
        if (year < 1000)
            throw new IllegalArgumentException("year must be four-digits");
        return (year % 4) == 0 && (year % 100 != 0 || year % 400 == 0);
    }
}

Related

  1. currentMonthFirstDay()
  2. dayForMonth(String pTime)
  3. daysInMonth(int month, boolean isLeapYear)
  4. daysInMonth(int year, int month)
  5. daysOfMonth(int year)
  6. daysOfMonth(int year, int month)
  7. firstDayOfLastMonth()