CODE 1 :
String startDate = "01/01/2004";
String endDate = "02/01/2005";
String st[] = startDate.split("/");
String end[] = endDate.split("/");
String mnth = st[0];String day = st[1];String yr = st[2];
String mnth1 = end[0];String day1 = end[1];String yr1 = end[2];
Calendar scal = Calendar.getInstance();
Calendar ecal = Calendar.getInstance();
scal.set(Integer.valueOf(yr), Integer.valueOf(mnth), Integer.valueOf(day));
ecal.set(Integer.valueOf(yr1), Integer.valueOf(mnth1), Integer.valueOf(day1));
long difInDays = ((ecal.getTime().getTime() - scal.getTime().getTime()) / (1000 * 60 * 60 * 24));
CODE 2 :
Date date1 = new Date(startDate);
Date date2 = new Date(endDate);
long diff = date2.getTime() - date1.getTime();
int days = (int)Math.ceil( diff/86400000 );
The DiffInDays Differs for the CODE 1 and CODE 2...,,
As the difference for the second line should be
397 which is correct in CODE 2
but in CODE 1 it gives a diferent value....!!!
Is this Difference a Issue in
Java version...???
StartDate EndDate DiffInDays
CODE 1: 01/01/2004 01/31/2005 396
CODE 2: 01/01/2004 02/01/2005 394
CODE 2: 01/01/2004 01/31/2005 396
CODE 2: 01/01/2004 02/01/2005 397