0

I am trying to compare dates in two different formats:

Tue Jul 01 00:12:14 EST 2014

which is created using the function:

private Date getDate (int day, int month, int year){
    Calendar calendar = Calendar.getInstance();
    calendar.setLenient(false);
    calendar.set(year, month-1, day);
    Date date = calendar.getTime();
    return date;
}

and

2014-07-01

After comparing these two dates, I would like the output to show that they are equal. However I BELIEVE, because of the timestamp in the 1st Date, they are not being determined as equal.

Is my assumption correct?

If so, is there a way that I could convert the first date into the second? The second Date is being retrieved from an SQL database where the variable is DATE.

Thank you for your help.

6 Answers 6

4

It sounds like you are comparing a java.util.Date (an instant in time) with a java.sql.Date (an instant in time whose time of day is midnight).

Arithmetic rounding must deal with the local timezones, making it more complex than you might first think.

The simplest way to compare the two would be to use a data formatter and compare the output:

SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
if (f.format(date1).equals(f.format(date2))) {
    // the two dates are on the same "day"
}
Sign up to request clarification or add additional context in comments.

Comments

1

java.sql.Date Has Zero Time

The documentation explains that a java.sql.Date has its time portion set to zero (UTC), meaning midnight.

So when comparing to a java.util.Date with a non-zero time-of-day, the two will not be equal.

LocalDate

So much easier using Joda-Time of the new java.time package in Java 8. Both offer a LocalDate class that ignores time-of-day.

LocalDate x = new LocalDate( 2014, 5, 6 );
LocalDate y = new LocalDate( 2014, 5, 6 );
boolean same = x.equals( y );

To convert your java.sql.Date to a Joda-Time LocalDate, pass it to the constructor of New LocalDate. You may need to also pass DateTimeZone.UTC to be sure it is not interpreted by your JVM's default time zone.

Comments

0

Is my assumption correct?

Yes, your assumption is correct. Two Date instances are correct if both their getTime() results are the same

from Date.java

public boolean equals(Object obj) {
    return obj instanceof Date && getTime() == ((Date) obj).getTime();
}

Converting just assumes you need to set the hours,minutes and seconds to 0:

calendar.set(Calendar.HOUR_OF_DAY,0);
calendar.set(Calendar.MINUTE,0);
calendar.set(Calendar.SECOND,0);

Comments

0

Assuming that both dates are in the same timezone and also assuming that the date equality criteria here is the day of the year, I believe you can just compare the date as strings.

To do that, you can use SimpleDateFormat to ensure both are in the same format.

Comments

0

I would suggest you convert both of them to one particular format and compare them using a Comparator.

Comments

0

If you need to check whether two dates are equal, the best way is to use compareTo method.

if(yesterday.compareTo(today) == 0) { 
    System.out.println("Given dates are same"); 
} else { 
    System.out.println("Given dates are different "); 
}

Read more: https://www.java67.com/2016/09/how-to-compare-two-dates-in-java.html#ixzz6uH5r1xE2

1 Comment

Apart from Date being long outdated (no pun intended), I can’t see it answers the question. At the same time it seems a tad more complicated than using the equals method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.