Here you can find the source of daysInMonth(int month, int year)
Parameter | Description |
---|---|
month | The month to be tested |
year | The year in which the month is to be tested |
public static int daysInMonth(int month, int year)
//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); } }