java-date-handling-examples

How to find the number of days between two Dates

Posted On
Posted By admin

In this post, I am going to demonstrate how you can find the number of days between two dates. The following code snippet demonstrates this:

 

Code snippet

</pre>
public static void main(String[] args) {
try {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
Date today = df.parse("2018-08-24");
Date future = df.parse("2018-09-04");
// Date future = getFutureDate();
int daysBetween = daysBetween(today,future);
System.out.println("days between = "+daysBetween);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

private static int daysBetween(Date date1, Date date2){
long time1 = date1.getTime(); //Returns no of milliseconds since January 1, 1970, 00:00:00 GMT
long time2 = date2.getTime();

long diffInMs = time2-time1;
int numOfMsInOneDay = 1000 * 60 * 60 * 24;

int daysBetween = (int) (diffInMs / numOfMsInOneDay);
return daysBetween;

}
<pre>

 

Explanation

The Date.getTime method is first used for each Date object.  This method returns the number of milliseconds elapsed since January 1, 1970, 00:00:00 GMT  for each Date object.  The difference in the milliseconds since January 1, 1970, 00:00:00 GMT is then calculated.

Next, the number of milliseconds in one day is calculated. Finally, the number of days between the two dates is calculated by dividing the difference in milliseconds with the number of milliseconds in one day.

 

Output

So if you run the code above, you will see the following output:

days between = 11
If you like this post, please do let me know via the comments box below.  You can also connect with me via my Facebook Page or subscribe to my Youtube channel!

Related Post

leave a Comment