Difference between two Java date instances

There are many ways you can find the difference between dates. The following is the simplest way to find the difference between two dates. You could just take the number of milliseconds between the two Date objects and divide by the number of milliseconds in 24 hours, with following rules :
1000 milliseconds = 1 second 60 seconds = 1 minute 60 minutes = 1 hour 24 hours = 1 day

That is, dateDifference in milliseconds/(1000 * 60 * 60 * 24) )

  1. This won't take time zones into consideration - Date is always in UTC
  2. This won't take daylight saving time into consideration (where there can be days which are only 23 hours long, for example)

The following Java program calculate the difference between two dates:

import java.text.SimpleDateFormat; import java.util.Date; public class TestClass { public static void main(String[] args) { SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy"); String inputString1 = "10 03 1995"; String inputString2 = "14 03 1995"; try{ Date oldDate = myFormat.parse(inputString1); Date newDate = myFormat.parse(inputString2); int diffInDays = (int)( (newDate.getTime() - oldDate.getTime()) / (1000 * 60 * 60 * 24) ); System.out.println(diffInDays); }catch(Exception ex){ System.out.println(ex); } } }

Date difference in java 8

It is very difficult to accurately calculate the difference between two dates in Java without using third party library unless you have time to develop your own java library like Joda , which purposefully takes these things into consideration. Thankfully, you don't need to worry because Java 8 got lucky third time. Java 8 made first attempt to upgrade this date/time API. Example
import java.time.LocalDate; import java.time.Period; public class TestClass { public static void main(String[] args) { LocalDate oldDate = LocalDate.of(1995, 05, 01); LocalDate now = LocalDate.now(); Period diff = Period.between(oldDate, now); System.out.printf("Date Difference is %d years, %d months and %d days old", diff.getYears(), diff.getMonths(), diff.getDays()); } }

Daylight Saving Time solution

DST (Daylight Saving Time) is the practice of setting the clocks forward 1 hour from standard time during the summer months, and back again in the fall, in order to make better use of natural daylight. The above solution caused issues for us when daylight savings time came around. Here's our working solution for all dates, without using JodaTime. It utilizes calendar objects:
import java.text.SimpleDateFormat; import java.util.Date; import java.util.Calendar; public class TestClass { public static void main(String[] args) { Calendar calendar1 = Calendar.getInstance(); Calendar calendar2 = Calendar.getInstance(); calendar1.set(1995, 03, 02); calendar2.set(1995, 03, 06); int diff = dateFiff(calendar1,calendar2); System.out.println(diff); } public static int dateFiff(Calendar date1, Calendar date2){ if (date1.get(Calendar.YEAR) == date2.get(Calendar.YEAR)) { return Math.abs(date1.get(Calendar.DAY_OF_YEAR) - date2.get(Calendar.DAY_OF_YEAR)); } else { if (date2.get(Calendar.YEAR) > date1.get(Calendar.YEAR)) { Calendar temp = date1; date1 = date2; date2 = temp; } int resultDays = 0; int dayOneOriginalYearDays = date1.get(Calendar.DAY_OF_YEAR); while (date1.get(Calendar.YEAR) > date2.get(Calendar.YEAR)) { date1.add(Calendar.YEAR, -1); resultDays += date1.getActualMaximum(Calendar.DAY_OF_YEAR); } return resultDays - date2.get(Calendar.DAY_OF_YEAR) + dayOneOriginalYearDays ; } } }