Here you can find the source of isSameMonth(Date date1, Date date2)
public static boolean isSameMonth(Date date1, Date date2)
//package com.java2s; /*/*from w ww. j av a 2 s. co m*/ Copyright (c) 2012,2013 Mirco Attocchi This file is part of WebAppCommon. WebAppCommon is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. WebAppCommon 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with WebAppCommon. If not, see <http://www.gnu.org/licenses/>. */ import java.util.Calendar; import java.util.Date; public class Main { public static boolean isSameMonth(Date date1, Date date2) { if (date1 == null || date2 == null) { throw new IllegalArgumentException("The dates must not be null"); } Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); return isSameDay(cal1, cal2); } public static boolean isSameMonth(Calendar cal1, Calendar cal2) { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException("The dates must not be null"); } return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH)); } /** * <p> * Checks if two calendars represent the same day ignoring time. * </p> * * @param cal1 * the first calendar, not altered, not null * @param cal2 * the second calendar, not altered, not null * @return true if they represent the same day * @throws IllegalArgumentException * if either calendar is <code>null</code> */ public static boolean isSameDay(Calendar cal1, Calendar cal2) { if (cal1 == null || cal2 == null) { throw new IllegalArgumentException("The dates must not be null"); } return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR)); } /** * <p> * Checks if two dates are on the same day ignoring time. * </p> * * @param date1 * the first date, not altered, not null * @param date2 * the second date, not altered, not null * @return true if they represent the same day * @throws IllegalArgumentException * if either date is <code>null</code> */ public static boolean isSameDay(Date date1, Date date2) { // if (date1 == null || date2 == null) { // throw new IllegalArgumentException("The dates must not be null"); // } if (date1 == null || date2 == null) { return false; } Calendar cal1 = Calendar.getInstance(); cal1.setTime(date1); Calendar cal2 = Calendar.getInstance(); cal2.setTime(date2); return isSameDay(cal1, cal2); } }