Java Day Between getDaysDifference(final Date begin, final Date end)

Here you can find the source of getDaysDifference(final Date begin, final Date end)

Description

Calculate diff in days between dates.

License

Open Source License

Parameter

Parameter Description
begin a parameter
end a parameter

Declaration

public static long getDaysDifference(final Date begin, final Date end) 

Method Source Code

//package com.java2s;
/**// w w w .  java2s .c o 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 begin
     * @param end
     * @return
     */
    public static long getDaysDifference(final Date begin, final Date end) {

        long milliseconds;
        long days;

        final Calendar calendarBegin = new GregorianCalendar();
        final Calendar calendarEnd = new GregorianCalendar();

        calendarBegin.setTime(begin);
        calendarBegin.set(Calendar.HOUR_OF_DAY, 0);
        calendarBegin.set(Calendar.MINUTE, 0);
        calendarBegin.set(Calendar.SECOND, 0);
        calendarBegin.set(Calendar.MILLISECOND, 0);

        calendarEnd.setTime(end);
        calendarEnd.set(Calendar.HOUR_OF_DAY, 0);
        calendarEnd.set(Calendar.MINUTE, 0);
        calendarEnd.set(Calendar.SECOND, 0);
        calendarEnd.set(Calendar.MILLISECOND, 0);

        milliseconds = calendarEnd.getTime().getTime()
                - calendarBegin.getTime().getTime();

        days = milliseconds / 86400000;

        return days;
    }
}

Related

  1. getDaysBetween(Date start, Date end)
  2. getDaysBetween(Date startDate, Date endDate)
  3. getDaysBetween(Date startDate, Date endDate)
  4. getDaysBetweenDate(Date begin, Date end)
  5. getDaysDiff(Date startDate, Date endDate)
  6. getDiffBetweenQuarter(Date latestDate, Date current)
  7. getDiffDays(Date date1, Date date2)
  8. getDiffDays(String startDate, String endDate)
  9. getDifferDays(Date d1, Date d2)