Java Day Between getDifferencesBetweenIndicateDays( final Date minorDate, final Date majorDate)

Here you can find the source of getDifferencesBetweenIndicateDays( final Date minorDate, final Date majorDate)

Description

Calculate diff in days between dates.

License

Open Source License

Parameter

Parameter Description
minorDate a parameter
majorDate a parameter

Declaration

public static int getDifferencesBetweenIndicateDays(
        final Date minorDate, final Date majorDate) 

Method Source Code

//package com.java2s;
/**/*w w  w . j av  a2s .co  m*/
 * Vulpe Framework - Quick and Smart ;)
 * Copyright (C) 2011 Active Thread
 *
 * Este programa ? software livre; voc? pode redistribu?-lo e/ou
 * modific?-lo sob os termos da Licen?a P?blica Geral GNU, conforme
 * publicada pela Free Software Foundation; tanto a vers?o 2 da
 * Licen?a como (a seu crit?rio) qualquer vers?o mais nova.
 *
 * Este programa ? distribu?do na expectativa de ser ?til, mas SEM
 * QUALQUER GARANTIA; sem mesmo a garantia impl?cita de
 * COMERCIALIZA??O ou de ADEQUA??O A QUALQUER PROP?SITO EM
 * PARTICULAR. Consulte a Licen?a P?blica Geral GNU para obter mais
 * detalhes.
 *
 * Voc? deve ter recebido uma c?pia da Licen?a P?blica Geral GNU
 * junto com este programa; se n?o, escreva para a Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

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

public class Main {
    /**
     * Calculate diff in days between dates.
     *
     * @param minorDate
     * @param majorDate
     * @return
     */
    public static int getDifferencesBetweenIndicateDays(
            final Date minorDate, final Date majorDate) {
        int numDiff = 0;

        if (majorDate.after(minorDate)) {
            final Calendar calendarMinor = new GregorianCalendar();
            calendarMinor.setTime(minorDate);
            final int dayYearMinor = calendarMinor
                    .get(Calendar.DAY_OF_YEAR);
            final int yearMinor = calendarMinor.get(Calendar.YEAR);

            final Calendar calendarMajor = new GregorianCalendar();
            calendarMajor.setTime(majorDate);
            int dayYearMaior = calendarMajor.get(Calendar.DAY_OF_YEAR);
            final int yearMaior = calendarMajor.get(Calendar.YEAR);

            if (yearMinor < yearMaior) {
                int auxYear = yearMinor;
                final Calendar calendarAux = calendarMinor;
                while (auxYear < yearMaior) {
                    dayYearMaior += calendarAux
                            .getActualMaximum(Calendar.DAY_OF_YEAR);
                    ++auxYear;
                    calendarAux.set(Calendar.YEAR, auxYear);
                }
            }

            numDiff = (dayYearMaior - dayYearMinor);
        }

        return numDiff;
    }
}

Related

  1. getDifferDays(Date d1, Date d2)
  2. getDifferDays(String date1, String date2)
  3. getDifference(Date d1, Date d2)
  4. getDifferenceInDays(final Date startDate, final Date endDate)
  5. getDifferenceOfDays(String dateFromStr, String dateToStr, String dateFormat)
  6. getDiffMilliSeconds(Date form, Date to)
  7. getManyWeeksDifference(Date a, Date b)
  8. getMinuteDiffByTime(Date time1, Date time2)
  9. getMinutesDifference(final Date begin, final Date end)