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

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

Description

get Last Day In Month

License

Apache License

Parameter

Parameter Description
month a parameter
year a parameter

Return

The number of days in the given month

Declaration

public static int getLastDayInMonth(int month, int year) 

Method Source Code

//package com.java2s;
/*//from   ww  w . ja v  a 2s  .  c om
 * Copyright 2013 Georges Stephan
 * 
 * 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.
 */

public class Main {
    /**
     * 
     * @param month
     * @param year
     * @return The number of days in the given month
     */
    public static int getLastDayInMonth(int month, int year) {
        int NumberOfDays = 0;
        switch (month) {
        case 0:// January
            NumberOfDays = 31;
            break;
        case 1:// February
            if ((((year % 4) == 0) && ((year % 100) != 0)) || ((year % 400) == 0)) {
                NumberOfDays = 29;
            } else {
                NumberOfDays = 28;
            }
            break;
        case 2:// March
            NumberOfDays = 31;
            break;
        case 3:// April
            NumberOfDays = 30;
            break;
        case 4:// May
            NumberOfDays = 31;
            break;
        case 5:// June
            NumberOfDays = 30;
            break;
        case 6:// July
            NumberOfDays = 31;
            break;
        case 7:// August
            NumberOfDays = 31;
            break;
        case 8:// September
            NumberOfDays = 30;
            break;
        case 9:// October
            NumberOfDays = 31;
            break;
        case 10:// November
            NumberOfDays = 30;
            break;
        case 11:// December
            NumberOfDays = 31;
            break;
        }
        return (NumberOfDays);
    }
}

Related

  1. getDaysInMonth(int year, int month)
  2. getDaysInMonths()
  3. getDaysOfMonth(Date startdate, Date enddate, String month)
  4. getFirstDayOfMonth(int year, int month)
  5. getFirstdayOfMonth(String year, String month)
  6. getLastDayOfMonth(int year, int month)
  7. getLastDayOfMonth(String monthInitials)
  8. getMaxDayByMonth(String month)
  9. getMaxMonthDay(int year, int month)