Java Month of Year daysInMonth(int month, int year)

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

Description

Returns the number of days in the month given for a given year.

License

Apache License

Parameter

Parameter Description
month The month to be tested
year The year in which the month is to be tested

Return

the number of days in the given month on the given year.

Declaration

public static int daysInMonth(int month, int year) 

Method Source Code

//package com.java2s;
/*//from w w w  . ja  va2 s  . c  om
 * Copyright (C) 2009 JavaRosa
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

import java.util.Calendar;

public class Main {
    /**
     * Returns the number of days in the month given for
     * a given year.
     * @param month The month to be tested
     * @param year The year in which the month is to be tested
     * @return the number of days in the given month on the given
     * year.
     */
    public static int daysInMonth(int month, int year) {
        if (month == Calendar.APRIL || month == Calendar.JUNE
                || month == Calendar.SEPTEMBER
                || month == Calendar.NOVEMBER) {
            return 30;
        } else if (month == Calendar.FEBRUARY) {
            return 28 + (isLeap(year) ? 1 : 0);
        } else {
            return 31;
        }
    }

    /**
     * Determines whether a year is a leap year in the
     * proleptic Gregorian calendar.
     * 
     * @param year The year to be tested
     * @return True, if the year given is a leap year, 
     * false otherwise.
     */
    public static boolean isLeap(int year) {
        return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
    }
}

Related

  1. currentYearMonth()
  2. dayOfYear(int year, int month, int day)
  3. dayOfYear(int year, int month, int day)
  4. daysInMonth(final int month, final int year)
  5. daysInMonth(final int year, final int month)
  6. daysInMonth(int month, int year)
  7. daysInMonth(int year, int month)
  8. daysInMonth(int year, int month)
  9. get_NextSunday_YearMonthDay_integerArray()