[ Back | Previous | Next ]

How to calculate the difference of an attribute between two dates?

Package:
java.util.*
Product:
JDK
Release:
1.1.x
Related Links:
Calendar
EventObject
Hashtable
Locale
Properties
PropertyResourceBundle
TimeZone
Comment:
/**
 * This method was created in VisualAge.
 * @return int
 * @param calendarItem float
 * @param d1 java.util.Date
 * @param d2 java.util.Date
 */
public static int calcDateDifference(float calendarItem, Date d1, Date d2) {
 int x = 1000;
 if (calendarItem == Calendar.DAY_OF_MONTH) {
  x = x * 60 * 60 * 24;
 } else
  if (calendarItem == Calendar.HOUR_OF_DAY) {
   x = x * 60 * 60;
  } else
   if (calendarItem == Calendar.MINUTE) {
    x = x * 60;
   } else
    return 1;
 return (int) ((d2.getTime() - d1.getTime()) / (x));
 // seconds in 1 day   
}
1