Java Date Compare by Month isSameMonth(final Date d1, final Date d2)

Here you can find the source of isSameMonth(final Date d1, final Date d2)

Description

is Same Month

License

Apache License

Parameter

Parameter Description
d1 a date
d2 the other date

Return

True if the dates represent the same month (year, month) independent of the days, hours, minutes etc.

Declaration

public static boolean isSameMonth(final Date d1, final Date d2) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

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

public class Main {
    /**//ww  w .j  ava  2 s.co  m
     * @param d1 a date
     * @param d2 the other date
     * @return True if the dates represent the same month (year, month) independent of the days, hours, minutes etc.
     */
    public static boolean isSameMonth(final Date d1, final Date d2) {
        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);
    }
}

Related

  1. isSameMonth(Date date1, Date date2)
  2. isSameMonth(Date date1, Date date2)
  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)