Here you can find the source of dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate)
Calendar
objects
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 |
Parameter | Description |
---|---|
IllegalArgumentException | if any argument is invalid |
public static int dateDiff(long dateUtilitiesUnitField, Calendar firstDate, Calendar secondDate) throws IllegalArgumentException
//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); } }