3

I am having trouble comparing dates in Java I have tried:

(today == actDate)

(today.equals(actDate))

Both always seem to evaluate to false:

enter image description here

In the image above the first date is today and the second is actDate.

Both are date objects:

Date today = new Date(System.currentTimeMillis());

Date actDate = new Date(taskHours.get(j).getDate().getTime());

Then I tried using compareTo, but this appears to return a 1 if the date is greater and a -1 if lower.

What am I doing wrong?

3
  • 1
    The dates seem different... Commented Dec 17, 2013 at 15:39
  • The first one has both 2013-05-03 Commented Dec 17, 2013 at 15:39
  • 2
    Are they equal at the millisencond? Commented Dec 17, 2013 at 15:40

6 Answers 6

3

java.util.Date and java.sql.Date differ. This might be the reason.

Sign up to request clarification or add additional context in comments.

3 Comments

THis appears to be the reason. Thanks. Would comparing the strings work?
@DavidTunnell Give it a try.
(!today.toString().equals(actDate.toString())) works, Thanks!
2

your problem is Date today = new Date(System.currentTimeMillis());. With this you use the current system-time in milliseconds , which are stored in the date object. Your second date doesn't hold the information with milliseconds and this why every comparison fails.

1 Comment

You should use Calendar to clear specific unwanted components of your date, typically to round backwards (usually to the previous midnight or to the previous whole second or minute) or to set the same day, month and year to compare only the hour of the day.
2

Working with dates is a bit tricky. You need to use compareTo as JDBC works with java.sql.Timestamp and within application you usually work with java.util.Date. Two objects are (usually) not considered to be equal if they are instances of two different classes (even thou they might be from the same hierarchy).

What you want to do is:

public boolean isSameDate(Date date1, Date date2) {
    return date1.compareTo(date2) == 0;
}

If you want to make your life easier when null values come in play, use commons-lang:

public boolean isSameDate(Date date1, Date date2) {
    return ObjectUtils.compare(date1, date2) == 0;
}

If you want to compare just the date information (not the time), then I suggest you to use Joda Time library (namely LocalDate class). Converting timestamps to calendar date objects is not a straghtforward (one line of code) task with standard Java components.

Comments

1

You can use the Date#after(Date when) and Date#before(Date otherDate) methods to compare dates and achive some order.

Comments

1

You are comparing 2 different dates (maybe seconds are different), so equals method return false which is expected. Using equals is the right way to compere them.

Comments

1

Use a Calendar object instead. And manually compare day, month and year separately.

Calendar cal1 = new Calendar();
cal1.setTime(date1);
Calendar cal2 = new Calendar();
cal2.setTime(date2);

boolean equal = true;
equal &= cal1.get(Calendar.DAY_OF_MONTH) == cal2.get(Calendar.DAY_OF_MONTH);
equal &= cal1.get(Calendar.MONTH) == cal2.get(Calendar.MONTH);
equal &= cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.