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

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

Description

Calculate diff in minutes between dates.

License

Open Source License

Parameter

Parameter Description
begin a parameter
end a parameter

Declaration

public static int getMinutesDifference(final Date begin, final Date end) 

Method Source Code

//package com.java2s;
/**//from  w w  w.j  a  v  a 2 s. 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 minutes between dates.
     *
     * @param begin
     * @param end
     * @return
     */
    public static int getMinutesDifference(final Date begin, final Date end) {
        return (int) getMillisecondDifference(begin, end) / 60000;
    }

    /**
     * Calculate diff in milliseconds between dates.
     *
     * @param begin
     * @param end
     * @return
     */
    public static long getMillisecondDifference(final Date begin,
            final Date end) {
        long milliseconds;

        final Calendar dtBegin = new GregorianCalendar();
        final Calendar dtEnd = new GregorianCalendar();

        dtBegin.setTime(begin);
        dtBegin.set(Calendar.HOUR_OF_DAY, dtBegin.get(Calendar.HOUR_OF_DAY));
        dtBegin.set(Calendar.MINUTE, dtBegin.get(Calendar.MINUTE));
        dtBegin.set(Calendar.SECOND, dtBegin.get(Calendar.SECOND));
        dtBegin.set(Calendar.MILLISECOND, dtBegin.get(Calendar.MILLISECOND));

        dtEnd.setTime(end);
        dtEnd.set(Calendar.HOUR_OF_DAY, dtEnd.get(Calendar.HOUR_OF_DAY));
        dtEnd.set(Calendar.MINUTE, dtEnd.get(Calendar.MINUTE));
        dtEnd.set(Calendar.SECOND, dtEnd.get(Calendar.SECOND));
        dtEnd.set(Calendar.MILLISECOND, dtEnd.get(Calendar.MILLISECOND));

        milliseconds = dtEnd.getTime().getTime()
                - dtBegin.getTime().getTime();
        return milliseconds;
    }
}

Related

  1. getDifferenceOfDays(String dateFromStr, String dateToStr, String dateFormat)
  2. getDifferencesBetweenIndicateDays( final Date minorDate, final Date majorDate)
  3. getDiffMilliSeconds(Date form, Date to)
  4. getManyWeeksDifference(Date a, Date b)
  5. getMinuteDiffByTime(Date time1, Date time2)
  6. getMonthDifference(Date from, Date to)
  7. getMonthsDifference(Date earlierDate, Date laterDate)
  8. getNumDaysDiffExclTime(Date date1, Date date2)
  9. getTimeDifference(Date d1, Date d2)