Java Date Compare by Month sameMonth(Date date1, Date date2)

Here you can find the source of sameMonth(Date date1, Date date2)

Description

Checks if the two given dates have the same month.

License

Open Source License

Parameter

Parameter Description
date1 a parameter
date2 a parameter

Declaration

public static boolean sameMonth(Date date1, Date date2) 

Method Source Code

//package com.java2s;

import java.util.Calendar;
import java.util.Date;

public class Main {
    /**//from   w w  w.j a  va2 s.  co  m
     * Checks if the two given dates have the same month.
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean sameMonth(Date date1, Date date2) {
        if (date1 == null || date2 == null) {
            return false;
        }
        return getMonth(date1) == getMonth(date2);
    }

    /**
     * Extracts the month value from the given date. Note that the month value is count from zero.
     * 
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        if (date == null) {
            return 0;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.MONTH);
    }
}

Related

  1. isSameMonth(Date date1, Date date2)
  2. isSameMonth(final Date d1, final Date d2)
  3. isSameMonth(final Date date1, final Date date2)
  4. monthsBetween(Date firstDate, Date secondDate)
  5. monthsBetween(final Date von, final Date bis)
  6. sameMonth(Date date1, Date date2)
  7. sameMonth(Date dateOne, Date dateTwo)
  8. sameMonthYear(Date firstDate, Date secondDate)