I am facing a weird issue with Java (version 6, 7, 8). In this code, I am trying to calculate the date diff between 2 dates and this code gives me date difference between 03/12/2018 and 01/04/2018 as 66.958 days and not exactly 67 days which is little surprising. I have not seen this behavior ever.
To prove this theory, you can try any date before 03/11/2018 which is simple math. Please advise what do you guys think. I have tried ChronoUnit class which is bundled with Java and it calculated the days correctly but just wondering what is going on here and if you have any opinion.
static void dateLogic1() throws ParseException{
String matYear1 = "03/11/2018";
String matYear2 = "03/12/2018";
String stlDate = "01/04/2018";
java.util.Date mtYear1 = new SimpleDateFormat("MM/dd/yyyy").parse(matYear1);
java.util.Date mtYear2 = new SimpleDateFormat("MM/dd/yyyy").parse(matYear2);
java.util.Date stYear = new SimpleDateFormat("MM/dd/yyyy").parse(stlDate);
long time1 = mtYear1.getTime();
long time2 = mtYear2.getTime();
long time4 = stYear.getTime();
double diff1 = (double) (mtYear1.getTime()-stYear.getTime()) /( 24 * 60 * 60 * 1000);
double diff2 = (double) (mtYear2.getTime()-stYear.getTime()) /( 24 * 60 * 60 * 1000);
System.out.println("Date Diff between "+ mtYear1 +" & "+ stYear +" is " + diff1 );
System.out.println("Date Diff between "+ mtYear2 +" & "+ stYear +" is " + diff2 );
}