Question: I'm working on getting the number of days between two dates using Java. When I give the application date as 27 it shows 0, and when I give as 26 it even shows 0. Is there anything wrong in my code? Any ideas would be greatly appreciated.

Here is my code 

```
  public static void main (String[] args) throws java.lang.Exception {            
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date currentDate = new Date();
        System.out.println("Current date:"+dateFormat.format(currentDate));
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strAppDate = "2014-10-28 15:11:30.0";
        Date appDate = formatter.parse(strAppDate);
        System.out.println("Application date :"+formatter.format(appDate));
        long diff = appDate.getTime() - currentDate.getTime();
        long diffDays = diff / (24 * 60 * 60 * 1000);
        System.out.println("Diff days :"+diffDays);
  }

```

Answer: 
You can try in this way. Use [TimeUnit.MILLISECONDS.toDays()](http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/TimeUnit.html#toDays%28long%29) 

```
long duration  = appDate.getTime() - currentDate.getTime();
long diffInSeconds = TimeUnit.MILLISECONDS.toSeconds(duration);
long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration);
long diffInHours = TimeUnit.MILLISECONDS.toHours(duration);
long diffInDays = TimeUnit.MILLISECONDS.toDays(duration); // number of days

```
If it's less than a day of difference, the result of the division operation will be zero, because that's how integer (and long integer) division works. You can try to divide by a `double` value:

```
double diffDays = diff / (24 * 60 * 60 * 1000.0);

```