Java Calendar Month getNumDaysInMonth(GregorianCalendar aCalendar)

Here you can find the source of getNumDaysInMonth(GregorianCalendar aCalendar)

Description

Gets the number of days in the current month.

License

Open Source License

Parameter

Parameter Description
aCalendar a GregorianCalendar to query

Return

the number of days in the current month taking leap years into consideration.

Declaration

public static int getNumDaysInMonth(GregorianCalendar aCalendar) 

Method Source Code

//package com.java2s;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class Main {
    private static final int[] mDaysInMonth = { 31, 28, 31, 30, 31, 30, 31,
            31, 30, 31, 30, 31 };/*w w w  .  j a va  2s.  c  o  m*/

    /**
     * Gets the number of days in the current month.
     *
     * @param aCalendar a GregorianCalendar to query
     *
     * @return the number of days in the current month taking leap years into
     * consideration.
     */
    public static int getNumDaysInMonth(GregorianCalendar aCalendar) {
        // Figure out how many days in month. Calendar has no good way of
        // doing this. So we build our own and use Calendar to check leap years.
        int month = aCalendar.get(Calendar.MONTH);
        int monthDays = mDaysInMonth[month];
        if (month == Calendar.FEBRUARY
                && aCalendar.isLeapYear(aCalendar.get(Calendar.YEAR))) {
            ++monthDays;
        }

        return monthDays;
    }
}

Related

  1. getMonthName(Calendar cal)
  2. getMonthStart(Calendar c)
  3. getMonthStart(Calendar cal)
  4. getMonthStr(Calendar cal)
  5. getNextMonth(Calendar calendar, int month)
  6. getStartOfMonth(Date day, Calendar cal)
  7. incrementMonth(Calendar cal)
  8. incrementMonthByVal(Calendar theCal, int val)
  9. intToCalendarMonth(int month)