Compare two times equals regardless of the date
import java.util.Calendar;
import java.util.Date;
public class Main {
public static boolean timeEquals(Date dtFirst, Date dtSecond,
boolean bIgnoreMilliseconds) {
boolean bReturn = false;
bReturn = (dtFirst == dtSecond);
if (!bReturn) {
if (dtFirst == null) {
// Two null dates are the same
bReturn = (dtSecond == null);
} else {
if (dtSecond != null) {
Calendar compCalendar;
int iHour;
int iMinute;
int iSecond;
int iMili;
compCalendar = Calendar.getInstance();
compCalendar.setTime(dtFirst);
iHour = compCalendar.get(Calendar.HOUR_OF_DAY);
iMinute = compCalendar.get(Calendar.MINUTE);
iSecond = compCalendar.get(Calendar.SECOND);
iMili = compCalendar.get(Calendar.MILLISECOND);
compCalendar.setTime(dtSecond);
bReturn = ((iHour == compCalendar.get(Calendar.HOUR_OF_DAY))
&& (iMinute == compCalendar.get(Calendar.MINUTE))
&& (iSecond == compCalendar.get(Calendar.SECOND)) && ((bIgnoreMilliseconds) || (iMili == compCalendar
.get(Calendar.MILLISECOND))));
}
}
}
return bReturn;
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Date Compare:
- Are two calendar objects represent the same local time.
- Are two dates or two calendars the same day
- Is a date the same day with another date
- Is a date after another date
- Is a date before another date
- Is date or calendar today's date
- Is a calendar or date after/before today or within a number of days in the future
- Compare two Date objects using compareTo
- Compares two dates are equals at day, month and year level, ignoring time
- Compare two times equals regardless of the date
- Compare two dates and times for equal
- Compare two Date