Java Month monthLength(int year, int month)

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

Description

count of days in the given month (one indexed) of the given year.

License

Apache License

Declaration

public static int monthLength(int year, int month) 

Method Source Code

//package com.java2s;
/*//from w  ww . ja  v  a2  s.c  o m
 * Copyright (C) 2006 Google Inc.
 *
 * 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.
 * All Rights Reserved.
 */

public class Main {
    /** count of days in the given month (one indexed) of the given year. */
    public static int monthLength(int year, int month) {
        switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            return 31;
        case 4:
        case 6:
        case 9:
        case 11:
            return 30;
        case 2:
            return isLeapYear(year) ? 29 : 28;
        default:
            throw new AssertionError(month);
        }
    }

    public static boolean isLeapYear(int year) {
        return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
    }
}

Related

  1. month2second(int month)
  2. month2String(int month)
  3. monthAlphaToNum(String str)
  4. monthDiff(String beforeTime, String endTime)
  5. monthFromDateValue(long x)
  6. monthMillis(int year, int monthNum)
  7. monthStringToInteger(String month)
  8. MonthText(int iIndex)
  9. monthToTerm(int month)