Java Utililty Methods Date Compare

List of utility methods to do Date Compare

Description

The list of methods to do Date Compare are organized into topic(s).

Method

booleancompareDateForSecond(Date src, Date target, int second)
compare Date For Second
Calendar targetTime = Calendar.getInstance();
targetTime.setTime(target);
targetTime.add(Calendar.SECOND, second);
Calendar srcTime = Calendar.getInstance();
srcTime.setTime(src);
return srcTime.compareTo(targetTime) <= 0;
intcompareDates(Date date1, Date date2)
Compares two given valid dates to return a number flag
if (date1 == null || date2 == null) {
    return -1;
int dateDifference = 0;
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(date1);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(date2);
...
intcompareDates(Date date1, Date date2)
Compares the two dates.
return date1.compareTo(date2);
intcompareDates(final Date dateA, final Date dateB)
Compares two dates ignoring time: just year, month, day.
final Calendar calendarA = GregorianCalendar.getInstance();
calendarA.setTime(dateA);
calendarA.set(Calendar.MILLISECOND, 0);
calendarA.set(Calendar.SECOND, 0);
calendarA.set(Calendar.MINUTE, 0);
calendarA.set(Calendar.HOUR_OF_DAY, 0);
final Calendar calendarB = GregorianCalendar.getInstance();
calendarB.setTime(dateB);
...
intcompareDates(final String format, final Date date1, final Date date2)
compare Dates
final DateFormat dateFormat = new SimpleDateFormat(format);
final String date1String = dateFormat.format(date1);
final String date2String = dateFormat.format(date2);
return date1String.compareTo(date2String);
intcompareDateSequence(String startDate, String endDate)
compare Date Sequence
java.util.Date start_Date = numberToDate(startDate);
java.util.Date end_Date = numberToDate(endDate);
int result = start_Date.compareTo(end_Date);
return result;
intcompareDatesOnly(long date1, long date2)
Compares the two dates.
c1.setTimeInMillis(date1);
c2.setTimeInMillis(date2);
int day1 = c1.get(Calendar.DAY_OF_MONTH);
int month1 = c1.get(Calendar.MONTH);
int year1 = c1.get(Calendar.YEAR);
int day2 = c2.get(Calendar.DAY_OF_MONTH);
int month2 = c2.get(Calendar.MONTH);
int year2 = c2.get(Calendar.YEAR);
...
intcompareDateTimeNull(Date d1, Date d2)
compare Date Time Null
if (d1 == null && d2 == null) {
    return 0;
if (d1 == null) {
    return -1;
if (d2 == null) {
    return 1;
...
intcompareDay(Date date, Date anotherDate)
compare Day
if (null == date && null == anotherDate) {
    return 0;
if (date == null) {
    date = new Date();
if (anotherDate == null) {
    anotherDate = new Date();
...
BooleancompareDay(Date date1, int compday)
compare Day
if (date1 == null)
    return false;
Date dateComp = getDateBeforeOrAfter(date1, compday);
Date nowdate = new Date();
if (dateComp.after(nowdate))
    return true;
else
    return false;
...