Java Utililty Methods Date Compare by Month

List of utility methods to do Date Compare by Month

Description

The list of methods to do Date Compare by Month are organized into topic(s).

Method

booleanisSameMonth(Date date1, Date date2)
Compare the two dates whether are in the same month.
if (date1 == null && date2 == null)
    return true;
if (date1 == null || date2 == null)
    return false;
Calendar cal1 = GregorianCalendar.getInstance();
cal1.setTime(date1);
Calendar cal2 = GregorianCalendar.getInstance();
cal2.setTime(date2);
...
booleanisSameMonth(Date date1, Date date2)
is Same Month
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);
...
booleanisSameMonth(final Date d1, final Date d2)
is Same Month
Calendar c1 = Calendar.getInstance();
c1.setTime(d1);
Calendar c2 = Calendar.getInstance();
c2.setTime(d2);
return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH);
booleanisSameMonth(final Date date1, final Date date2)
is Same Month
final Calendar cal1 = Calendar.getInstance();
cal1.setTime(date1);
final Calendar cal2 = Calendar.getInstance();
cal2.setTime(date2);
if (cal1.get(Calendar.YEAR) != cal2.get(Calendar.YEAR)) {
    return false;
return cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
...
intmonthsBetween(Date firstDate, Date secondDate)
Calculates number of month between two dates.
Date before = null;
Date after = null;
if (firstDate.after(secondDate)) {
    after = firstDate;
    before = secondDate;
} else {
    after = secondDate;
    before = firstDate;
...
IntegermonthsBetween(final Date von, final Date bis)
months Between
if (von == null || bis == null) {
    return null;
if (bis.before(von)) {
    return null;
final Calendar calStart = Calendar.getInstance();
calStart.setTime(von);
...
booleansameMonth(Date date1, Date date2)
Checks if the two given dates have the same month.
if (date1 == null || date2 == null) {
    return false;
return getMonth(date1) == getMonth(date2);
booleansameMonth(Date date1, Date date2)
same Month
Calendar c1 = Calendar.getInstance();
Calendar c2 = Calendar.getInstance();
c1.setTime(date1);
c2.setTime(date2);
return c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR) && c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH);
booleansameMonth(Date dateOne, Date dateTwo)
Test to see if two dates are in the same month
if ((dateOne == null) || (dateTwo == null)) {
    return false;
Calendar cal = Calendar.getInstance();
cal.setTime(dateOne);
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
cal.setTime(dateTwo);
...
booleansameMonthYear(Date firstDate, Date secondDate)
same Month Year
Calendar first = Calendar.getInstance();
first.setTime(firstDate);
Calendar second = Calendar.getInstance();
second.setTime(secondDate);
return sameMonthYear(first, second);