Class to calculate differences in two dates

Many time we need to calculate differences in two dates in different units like year, months, days, hour, minutes, seconds. For this here is one simple utility java class to do this. It has following methods to calculate differences.

  • public double getSecondsdifference(Date oldDate, Date newDate)
  • public double getMinutesdifference(Date oldDate, Date newDate)
  • public double getHoursdifference(Date oldDate, Date newDate)
  • public double getDaysdifference(Date oldDate, Date newDate)
  • public double getWeaksdifference(Date oldDate, Date newDate)
  • public double getMonthsdifference(Date oldDate, Date newDate)
  • public double getYearsdifference(Date oldDate, Date newDate)
  • public static long getMilliSecDifference(Date oldDate, Date newDate)
import java.util.Date;
public class DateDiffUtil {
/**
* Gets the seconds difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the seconds difference
*/
public double getSecondsdifference(Date oldDate, Date newDate){
long diff = getMilliSecDifference(oldDate, newDate);
return diff/( 1000.0);
}
/**
* Gets the minutes difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the minutes difference
*/
public double getMinutesdifference(Date oldDate, Date newDate){
long diff = getMilliSecDifference(oldDate, newDate);
return diff/( 60.0 * 1000);
}
/**
* Gets the hours difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the hours difference
*/
public double getHoursdifference(Date oldDate, Date newDate){
long diff = getMilliSecDifference(oldDate, newDate);
return diff/(60.0 * 60 * 1000);
}
/**
* Gets the days difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the days difference
*/
public double getDaysdifference(Date oldDate, Date newDate){
long diff = getMilliSecDifference(oldDate, newDate);
return diff/(60.0 * 60 * 1000 * 24);
}
/**
* Gets the weaks difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the weaks difference
*/
public double getWeaksdifference(Date oldDate, Date newDate){
long diff = getMilliSecDifference(oldDate, newDate);
return diff/(60.0 * 60 * 1000 * 24 * 7);
}
/**
* Gets the months difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the months difference
*/
public double getMonthsdifference(Date oldDate, Date newDate){
long diff = getMilliSecDifference(oldDate, newDate);
return diff/(60.0 * 60 * 1000 * 24 * 30.41666666);
}
/**
* Gets the years difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the years difference
*/
public double getYearsdifference(Date oldDate, Date newDate){
long diff = getMilliSecDifference(oldDate, newDate);
return diff/(60.0 * 60 * 1000 * 24 * 365);
}
/**
* Gets the milli sec difference.
*
* @param oldDate the old date
* @param newDate the new date
* @return the milli sec difference
*/
public static long getMilliSecDifference(Date oldDate, Date newDate){
if(oldDate == null || newDate == null)
throw new NullPointerException();
return newDate.getTime()-oldDate.getTime();
}
}