Here you can find the source of getStartAndEndOfMonth(int month, int year)
public static Calendar[] getStartAndEndOfMonth(int month, int year)
//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); } }