Comparing only dates not working properly in Java

323 views Asked by At

I'm having two dates for Java util package. Both of them are showing the same date. When I compare both, it shows as wrong. Here is my code and the log

Date date1=new SimpleDateFormat("dd/MM/yy").parse(lastCall);  
logger.info(date1); -->Mon Sep 18 00:00:00 AST 2017
Date todayDate = new Date(); -->Mon Sep 18 12:36:53 AST 2017
System.out.println("Today date :"+todayDate);

if(date1.after(todayDate)){
     System.out.println("Date1 is after Date2");
}

if(date1.before(todayDate)){
    System.out.println("Date1 is before Date2");
}

if(date1.equals(todayDate)){
    System.out.println("Date1 is equal Date2");
}

It goes into before block and prints

  System.out.println("Date1 is before Date2");
1

There are 1 answers

4
yılmaz On

Date parts are same but time parts are different. So according to comparison of hours, minutes and seconds you end up with this result.