Java Calendar Different getDateDiff(Calendar d1, Calendar d2)

Here you can find the source of getDateDiff(Calendar d1, Calendar d2)

Description

Computes the number of minutes between two Dates.

License

Open Source License

Parameter

Parameter Description
d1 the first Date
d2 the second Date

Return

the absolute value of the number of minutes

Declaration

public static long getDateDiff(Calendar d1, Calendar d2) 

Method Source Code

//package com.java2s;
/*/*w w w . j  a  va2 s . c o  m*/
   Animal Shelter Manager
   Copyright(c)2000-2011, R. Rawson-Tetley
    
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of
   the License, or (at your option) any later version.
    
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
   GNU General Public License for more details.
    
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston
   MA 02111-1307, USA.
    
   Contact me by electronic mail: bobintetley@users.sourceforge.net
*/

import java.util.Calendar;

public class Main {
    /**
     * Computes the number of minutes between
     * two Dates.
     *
     * @param  d1  the first Date
     * @param  d2  the second Date
     *
     * @return the absolute value of the number
     * of minutes
     *
     * @exception  NullPointerException if either
     * Date null
     */
    public static long getDateDiff(Calendar d1, Calendar d2) {
        //assert d1 != null && d2 != null;
        if ((d1 == null) || (d2 == null)) {
            throw new NullPointerException("d1 or d2 null");
        }

        // get the difference in milliseconds and
        // take the absolute value
        long diff = d1.getTime().getTime() - d2.getTime().getTime();

        // convert milliseconds to seconds 
        // and then to minutes
        long res = diff / (1000 * 60);

        return res;
    }
}

Related

  1. diffDay(Calendar calendar, int day)
  2. diffDays(Calendar startDate, Calendar endDate)
  3. differenceInDays(Calendar date1, Calendar date2)
  4. formatTimeDiff(Calendar ref, Calendar now, boolean ignoreMS)
  5. getDateDiff(Calendar c1, Calendar c2)
  6. getDateDiff(Date d1, Date d2, int gregorianCalendarUnits)
  7. getDaysDifference(Calendar refDate, Date date)
  8. getDifference(int constant, Calendar first, Calendar second)
  9. getDifference(int field, Calendar aCalX, Calendar aCalY)