Java Calendar Different dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)

Here you can find the source of dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)

Description

Calculate the difference, in DateUtilitities field units, for any two Calendar objects

License

Open Source License

Parameter

Parameter Description
dateUtilitiesUnitField - the unit of measure (e.g., DateUtilities.DAY_UNITS, DateUtilities.HOUR_UNITS, etc.)
firstDate - a <code>Calendar</code> object
secondDate - a <code>Calendar</code> object

Exception

Parameter Description
IllegalArgumentException if any argument is invalid

Return

the difference in DateUtilities units as a positive whole number

Declaration

public static int dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;
/*/*  ww w  .j  a v a  2  s.c o m*/
 * (C) 2007 - James G. Lombardo dba The Byteshop.Net
 * 
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 */

import java.util.Calendar;

public class Main {
    /**
     * Calculate the difference, in DateUtilitities field units, for any two <code>Calendar</code> objects
     * @param dateUtilitiesUnitField - the unit of measure (e.g., DateUtilities.DAY_UNITS, DateUtilities.HOUR_UNITS, etc.)
     * @param firstDate - a <code>Calendar</code> object
     * @param secondDate - a <code>Calendar</code> object
     * @return the difference in DateUtilities units as a positive whole number
     * @throws IllegalArgumentException if any argument is invalid
     */
    public static int dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)
            throws IllegalArgumentException {
        long diff = Math.abs(firstDate.getTimeInMillis() - secondDate.getTimeInMillis());
        double diffAmt = (double) diff / dateUtilitiesUnitField;

        return (int) Math.round(diffAmt);
    }
}

Related

  1. countDiffMonth(Calendar cBegin, Calendar cEnd, boolean isRoundUp)
  2. datediff(Calendar c1, Calendar c2)
  3. dateDiff(int type, Calendar fromDate, Calendar toDate)
  4. dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
  5. dateDiff(int type, Calendar fromDate, Calendar toDate, boolean future)
  6. dateDifferenceInMin(Calendar startDate, Calendar stopDate)
  7. diff(Calendar value1, Calendar value2, int millisInCondition)
  8. diffDay(Calendar calendar, int day)
  9. diffDays(Calendar startDate, Calendar endDate)