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

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

Description

get Start And End Of Month

License

Open Source License

Declaration

public static Calendar[] getStartAndEndOfMonth(int month, int year) 

Method Source Code

//package com.java2s;
/*/* w  w  w  .  j a  v a  2s .  c  o m*/
 * Copyright (C) 2007-2010 Castafiore
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see<http://www.gnu.org/licenses/>.
 */

import java.util.Calendar;

public class Main {
    public static Calendar[] getStartAndEndOfMonth(int month, int year) {
        Calendar startDate = Calendar.getInstance();
        startDate.set(year, month, 1, 0, 0);
        Calendar endDate = Calendar.getInstance();
        int days = getDaysInMonth(month, year);
        endDate.set(year, month, days, 23, 59);

        return new Calendar[] { startDate, endDate };
    }

    public static int getDaysInMonth(int month, int year) {
        if (month == 0) {
            return 31;
        } else if (month == 1) {
            if (isLeapYear(year)) {
                return 29;
            } else {
                return 28;
            }
        } else if (month == 2) {
            return 31;
        } else if (month == 3) {
            return 30;
        } else if (month == 4) {
            return 31;
        } else if (month == 5) {
            return 30;
        } else if (month == 6) {
            return 31;
        } else if (month == 7) {
            return 31;
        } else if (month == 8) {
            return 30;
        } else if (month == 9) {
            return 31;
        } else if (month == 10) {
            return 30;
        } else if (month == 11) {
            return 31;
        }
        return -1;
    }

    public static boolean isLeapYear(int year) {
        return (year % 4 == 0);
    }
}

Related

  1. getPreviousDateStringByMonth(int months)
  2. getPreviousDay(int year, int month, int day, int days)
  3. getPrevMonth(String divisionDate, int day, String outputFormat)
  4. getPreYearMonth()
  5. getSecondsOfMonth(int year, int month)
  6. getStrDayOfWeek(int _year, int _month, int _day)
  7. getThisMonth(int year, int monthIndex)
  8. getTime(int year, int month, int day)
  9. getTimeByYMD(int year, int month, int day)