-2

I am comparing two same date and output that I am getting is -1. As far as I know it should be 0 for same date.

The image from the debug mode is attached here. How could this issue will be solved?

enter image description here

paudel.sulav
  • 162
  • 1
  • 12
  • could you post the code please? – Thecave3 Jun 06 '17 at 10:56
  • 1
    Answer is easy: these dates aren't the same. – Tom Jun 06 '17 at 10:57
  • I have attached the variable value from debug mode. I am getting the same date in debug for both the date that I am comparing. – paudel.sulav Jun 06 '17 at 11:00
  • i believe that you are comparing date with different time so it return negative – soorapadman Jun 06 '17 at 11:08
  • 1
    It seems from your screen shot that the `Date` objects are equal down to the seconds. One possible explanation is their milliseconds differ. One way to check is to print out `todayDate.getTime()` and similarly for the other date. This will give you numbers with many digits (milliseconds since the epoch), and the curious thing is whether the numbers differ within the last three digits. – Ole V.V. Jun 06 '17 at 13:10
  • 1
    A tip for you, if you can (which you can), prefer the modern `LocalDate` class for dates where the time of day doesn’t matter. – Ole V.V. Jun 06 '17 at 13:12
  • 1
    Best to give us a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). Then we would not be referred to mere guessing. – Ole V.V. Jun 06 '17 at 13:13

1 Answers1

0

You can use this post to compare two date without time like this.

if (removeTime(questionDate).equals(removeTime(today)) 
  ...

public Date removeTime(Date date) {    
    Calendar cal = Calendar.getInstance();  
    cal.setTime(date);  
    cal.set(Calendar.HOUR_OF_DAY, 0);  
    cal.set(Calendar.MINUTE, 0);  
    cal.set(Calendar.SECOND, 0);  
    cal.set(Calendar.MILLISECOND, 0);  
    return cal.getTime(); 
}
Med Elgarnaoui
  • 1,612
  • 1
  • 18
  • 35