java - Why number of days between 2 dates are not coming up correctly? -


i trying number of days between 2 dates.

date1 : 21-jan-15

date2 : 23-jul-15

in local eclipse tomcat server (in india), getting number of days as: 183 following code:

calendar cal1 = new gregoriancalendar(); calendar cal2 = new gregoriancalendar();  date date1 = new date("21-jan-15"); date date2 = new date("23-jul-15");  cal1.settime(date1); cal2.settime(date2);  system.out.println("days= "+daysbetween(cal1.gettime(),cal2.gettime()));  public int daysbetween(date d1, date d2){          return (int)( (d2.gettime() - d1.gettime()) / (1000 * 60 * 60 * 24));  } 

when copy same java file linux server , run file there using javac , java commands, difference as: 182, 1 day less.

i have confirmed default timezone of linux server edt using command: date +%z

i confused here. not have joda library in application , can't use it. changes need make uniform result both in local environment , linux server? appreciated!

here solution using timeunit

public static void main(string[] args) {     date date1 = new date("21-jan-15");     date date2 = new date("23-jul-15");      long diffinmillies = date2.gettime() - date1.gettime();      system.out.println(timeunit.milliseconds.todays(diffinmillies)); } 

output:

182


Comments