package net.javaiq.examples.date;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
@author
public class MonthYearEqualityChecker {
public static boolean checkIfMonthAndYearAreEqual(Date firstDate, Date secondDate) {
boolean isEqual = false;
if (firstDate != null && secondDate != null) {
Calendar firstCal = Calendar.getInstance();
firstCal.setTime(firstDate);
Calendar secondCal = Calendar.getInstance();
secondCal.setTime(secondDate);
isEqual =
firstCal.get(Calendar.YEAR) == secondCal.get(Calendar.YEAR) && firstCal.get(Calendar.MONTH) ==
secondCal.get(Calendar.MONTH);
}
return isEqual;
}
@param
public static void main(String[] args) throws Exception {
Date firstDate = new SimpleDateFormat("MM-dd-yyyy").parse("12-10-2010");
Date secondDate = new SimpleDateFormat("MM-dd-yyyy").parse("12-18-2010");
boolean isEqual = checkIfMonthAndYearAreEqual(firstDate, secondDate);
if (isEqual) {
System.out.println("Month and year of the passed dates are equal.");
} else {
System.out.println("Month and year of the passed dates are not equal.");
}
secondDate = new SimpleDateFormat("MM-dd-yyyy").parse("12-18-2011");
isEqual = checkIfMonthAndYearAreEqual(firstDate, secondDate);
if (isEqual) {
System.out.println("Month and year of the passed dates are equal.");
} else {
System.out.println("Month and year of the passed dates are not equal.");
}
}
}