Java Date Compare compareToMonthByDate(Date startDate, Date endDate)

Here you can find the source of compareToMonthByDate(Date startDate, Date endDate)

Description

compare To Month By Date

License

Apache License

Declaration

public static int compareToMonthByDate(Date startDate, Date endDate) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.Calendar;

import java.util.Date;
import java.util.GregorianCalendar;

public class Main {

    public static int compareToMonthByDate(Date startDate, Date endDate) {
        Calendar startCal = new GregorianCalendar();
        Calendar endCal = new GregorianCalendar();
        startCal.setTime(startDate);//from  ww w  .j  ava2  s  . c om
        endCal.setTime(endDate);
        int a = compare(endCal, startCal, 2);
        return a;
    }

    public static int compare(Calendar c1, Calendar c2, int what) {
        int number = 0;
        switch (what) {
        case Calendar.YEAR:
            number = c1.get(Calendar.YEAR) - c2.get(Calendar.YEAR);
            break;
        case Calendar.MONTH:
            int years = compare(c1, c2, Calendar.YEAR);
            number = 12 * years + c1.get(Calendar.MONTH) - c2.get(Calendar.MONTH);
            break;
        case Calendar.DATE:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        default:
            number = (int) ((c1.getTimeInMillis() - c2.getTimeInMillis()) / (1000 * 60 * 60 * 24));
            break;
        }
        return number;
    }
}

Related

  1. compareTime(Date date1, Date date2)
  2. compareTime(Date first, Date second)
  3. compareTimesOfDay(Date time1, Date time2)
  4. compareTo(Date src, Date dest)
  5. compareToCal(Date sDate, Date eDate)
  6. compareTwoDate(Date beforeDate, Date afterDate)
  7. compareYear(java.util.Date date1, java.util.Date date2)
  8. compareYMD(Date begindate, Date enddate)
  9. compDate(Date date1, Date date2)