Thursday, June 13, 2019

Java :: Compare Two dates

If you have tried to compare dates in java , you would have experience trouble checking "equal".
This is because, date include a time component. There is a high chance that time component can be different.
But if we need to compare two dates, only looking at the "Year","Month","Day"
Then Time must be removed from the date.
Below example shows how to do that.

package com.date.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateConverter {

public static void main(String[] args) {
Date curDate = new Date();
System.out.println(curDate);
System.out.println(getDateWithoutTime(curDate));
}

/**
* Check whether First date is after or equal to Second date. When
* comparing the dates, the time element is removed before compare, else always
* not equal due to time component.
* Second Date | First Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is after or equal Second date and False
*         vice versa.
*/
public final static boolean isFirstDateAfterOrEqualToSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
Date firstDateWithoutTime = getDateWithoutTime(firstDate);
Date secondDateWithoutTime = getDateWithoutTime(secondDate);
return ((firstDateWithoutTime.after(secondDateWithoutTime))
|| (firstDateWithoutTime.equals(secondDateWithoutTime)));
}
return false;
}

/**
* Check whether First date is after Second date. When comparing the
* dates, the time element is removed before compare.
* Second Date | First Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is after Second date and False vice
*         versa.
*/
public final static boolean isFirstDateAfterSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
return (getDateWithoutTime(firstDate)).after(getDateWithoutTime(secondDate));
}
return false;
}

/**
* Check whether First date is before or equal to Second date. When
* comparing the dates, the time element is removed before compare, else always
* not equal due to time component.
* First Date | Second Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is before or equal Second date and False
*         vice versa.
*/
public final static boolean isFirstDateBeforeOrEqualToSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
Date firstDateWithoutTime = getDateWithoutTime(firstDate);
Date secondDateWithoutTime = getDateWithoutTime(secondDate);
return ((firstDateWithoutTime.before(secondDateWithoutTime))
|| (firstDateWithoutTime.equals(secondDateWithoutTime)));
}
return false;
}

/**
* Check whether First date is before to Second date. When comparing the
* dates, the time element is removed before compare, else always not equal due
* to time component.
* First Date | Second Date
*
* @param firstDate
*            First date to be compared.
* @param secondDate
*            Second date to be compared.
* @return True when First date is before Second date and False vice
*         versa.
*/
public final static boolean isFirstDateBeforeSecondDate(Date firstDate, Date secondDate) {
if (firstDate != null && secondDate != null) {
return (getDateWithoutTime(firstDate)).before(getDateWithoutTime(secondDate));
}
return false;
}

/**
* Check whether date has a time component.
*
* @param aDate
*            valid date object.
* @return True when there is not time value for date and False vice versa.
*/
public final static boolean isTimeEqualToZero(Date aDate) {
if (aDate != null) {
Calendar aCalendarDate = new GregorianCalendar();
aCalendarDate.setTime(aDate);

int theHour = aCalendarDate.get(Calendar.HOUR_OF_DAY);
int theMinute = aCalendarDate.get(Calendar.MINUTE);

return (theHour == 0 && theMinute == 0);
}

return false;
}

/**
* Get the Date without Time. In Other words,get only Year,Month,day component.
*
* @param aDate
*            Valid date object.
* @return Date without Time.
*/
public static final Date getDateWithoutTime(Date aDate) {
Date rtnDate = null;
if (aDate != null) {
Calendar aCalendarDate = new GregorianCalendar();

aCalendarDate.setTime(aDate);
aCalendarDate.set(GregorianCalendar.HOUR, 0);
aCalendarDate.set(GregorianCalendar.MINUTE, 0);
aCalendarDate.set(GregorianCalendar.SECOND, 0);
aCalendarDate.set(GregorianCalendar.MILLISECOND, 0);
aCalendarDate.set(GregorianCalendar.AM_PM, GregorianCalendar.AM);
rtnDate = aCalendarDate.getTime();
}
return rtnDate;
}
}

No comments:

Post a Comment