Note: this answer was written in 2011. I would recommend using java.time now instead of Joda Time.

Well to start with, you should only deal with them as strings when you have to. Most of the time you should work with them in a data type which actually describes the data you're working with.

I would recommend that you use Joda Time, which is a much better API than Date/Calendar. It sounds like you should use the LocalDate type in this case. You can then use:

int days = Days.daysBetween(date1, date2).getDays();
Answer from Jon Skeet on Stack Overflow
🌐
Baeldung
baeldung.com › home › java › java dates › difference between two dates in java
Difference Between Two Dates in Java | Baeldung
May 8, 2025 - The Time API in Java 8 represents a unit of date-time, e.g. seconds or days, using TemporalUnit interface. Each unit provides an implementation for a method named between() to calculate the amount of time between two temporal objects in terms of that specific unit.
🌐
How to do in Java
howtodoinjava.com › home › java date time › number of days between two dates in java
Number of Days between Two Dates in Java
October 15, 2024 - Internally, it is as simple as date1.toEpochDay() – date2.toEpochDay(). Please note that startDate is ‘inclusive‘, and endDate is ‘exclusive‘. LocalDate start = LocalDate.parse("2023-06-01"); // Inclusive LocalDate end = ...
🌐
BeginnersBook
beginnersbook.com › 2017 › 10 › java-8-calculate-days-between-two-dates
Java 8 – Calculate days between two dates
September 11, 2022 - import java.time.LocalDate; import java.time.temporal.ChronoUnit; public class Example { public static void main(String[] args) { String dateBeforeString = "2017-05-24"; String dateAfterString = "2017-07-29"; //Parsing the date LocalDate dateBefore = LocalDate.parse(dateBeforeString); LocalDate dateAfter = LocalDate.parse(dateAfterString); //calculating number of days in between long noOfDaysBetween = ChronoUnit.DAYS.between(dateBefore, dateAfter); //displaying the number of days System.out.println(noOfDaysBetween); } }
🌐
Medium
medium.com › @AlexanderObregon › javas-period-between-method-explained-c32f4cd996c6
Java’s Period.between() Method Explained | Medium
January 1, 2025 - The method calculates the gap between 2022-06-01 and 2024-12-17, breaking it into years, months, and days. This is useful when comparing two fixed dates or measuring durations between past events. When the start date comes after the end date, Period.between() produces negative values. import java.time.LocalDate; import java.time.Period; public class NegativePeriodExample { public static void main(String[] args) { LocalDate startDate = LocalDate.of(2025, 12, 15); // Start date LocalDate endDate = LocalDate.of(2024, 12, 17); // End date (current date of writing) Period result = Period.between(startDate, endDate); System.out.println("Difference: " + result.getYears() + " years, " + result.getMonths() + " months, " + result.getDays() + " days."); } }
Top answer
1 of 16
319

UPDATE

The original answer from 2013 is now outdated because some of the classes have been replaced. The new way of doing this is using the new java.time classes.

24-hour days

DateTimeFormatter dtf = DateTimeFormatter.ofPattern("dd MM yyyy");
String inputString1 = "23 01 1997";
String inputString2 = "27 04 1997";

try {
    LocalDateTime date1 = LocalDate.parse(inputString1, dtf).atStartOfDay();
    LocalDateTime date2 = LocalDate.parse(inputString2, dtf).atStartOfDay();
    long daysBetween = Duration.between(date1, date2).toDays();
    System.out.println ("Days: " + daysBetween);
} catch (DateTimeParseException e) {
    e.printStackTrace();
}

Calendar days

Note that the solution above counts days as generic chunks of 24 hours, not calendar days.

For calendar days, use java.time.temporal.ChronoUnit.DAYS and its between method.

long daysBetween = ChronoUnit.DAYS.between(date1, date2) ;

Original answer (outdated as of Java 8)

You are making some conversions with your Strings that are not necessary. There is a SimpleDateFormat class for it - try this:

SimpleDateFormat myFormat = new SimpleDateFormat("dd MM yyyy");
String inputString1 = "23 01 1997";
String inputString2 = "27 04 1997";

try {
    Date date1 = myFormat.parse(inputString1);
    Date date2 = myFormat.parse(inputString2);
    long diff = date2.getTime() - date1.getTime();
    System.out.println ("Days: " + TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS));
} catch (ParseException e) {
    e.printStackTrace();
}

There have been some discussions regarding the correctness of this code. It does indeed take care of leap years. However, the TimeUnit.DAYS.convert function loses precision since milliseconds are converted to days (see the linked doc for more info). If this is a problem, diff can also be converted by hand:

float days = (diff / (1000*60*60*24));

Note that this is a float value, not necessarily an int.

2 of 16
149

Simplest way:

public static long getDifferenceDays(Date d1, Date d2) {
    long diff = d2.getTime() - d1.getTime();
    return TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
}
🌐
Codemia
codemia.io › knowledge-hub › path › calculate_days_between_two_dates_in_java_8
Calculate days between two Dates in Java 8
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › find-the-duration-of-difference-between-two-dates-in-java
Find the duration of difference between two dates in Java - GeeksforGeeks
July 15, 2025 - Examples: Input: start_date = "10-01-2018 01:10:20", end_date = "10-06-2020 06:30:50" Output: 2 years, 152 days, 5 hours, 20 minutes, 30 seconds Input: start_date = "10-01-2019 01:00:00", end_date = "10-06-2020 06:00:00" Output: 1 years, 152 ...
Find elsewhere
🌐
Mkyong
mkyong.com › home › java8 › java 8 – how to calculate days between two dates?
Java 8 - How to calculate days between two dates? - Mkyong.com
February 13, 2020 - In Java 8, we can use ChronoUnit.DAYS.between(from, to) to calculate days between two dates.
🌐
Blogger
javarevisited.blogspot.com › 2021 › 05 › localdate-until-and-between-example-how.html
How to find number of days between two dates in Java 8 [LocalDate until() & between() Example ]
You can use the LocalDate.between() method to find the number of days between two dates in Java. This method is full proof and takes care of date-time nuisances like leap year and daylight saving time.
🌐
Stack Abuse
stackabuse.com › how-to-get-the-number-of-days-between-dates-in-java
How to Get the Number of Days Between Dates in Java
May 4, 2022 - This set of units provides unit-based access to manipulate a date, time, or date-time. ChronoUnit has only a few methods, and the one that interests us the most is the between() method. This method takes in two Temporal objects and calculates the amount of time between them.
🌐
How to do in Java
howtodoinjava.com › home › java date time › getting all dates between two dates in java
Getting All Dates Between Two Dates in Java
February 18, 2022 - LocalDate startDate = LocalDate.now(); LocalDate endDate = startDate.plusMonths(2); long numOfDays = ChronoUnit.DAYS.between(startDate, endDate); List<LocalDate> listOfDates = LongStream.range(0, numOfDays) .mapToObj(startDate::plusDays) .collect(Collectors.toList()); System.out.println(listOfDates.size()); // 61 · To get all dates from startDate to endDate in Java 7, we must write our own custom logic as there is very limited API support in JDK.
🌐
Medium
medium.com › @a.sofuoglu1 › calculating-work-days-between-two-dates-in-java-aab1cc966d3
Calculating Work Days Between Two Dates in Java | by Latif sofuoğlu | Medium
May 28, 2023 - Here’s an example of how you can use the getWorkDaysBetweenTwoDate() method: ... Calculating the workdays between two dates is a common requirement in various applications. The provided code snippet offers a simple and efficient way to calculate ...
🌐
BeginnersBook
beginnersbook.com › 2013 › 04 › number-of-days-calculation-between-two-dates
Java – Calculate number of days between two dates
SimpleDateFormat myFormat = new SimpleDateFormat("MM/dd/yyyy"); String dateBeforeString = "01/31/2014"; String dateAfterString = "02/02/2014"; ... I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner. ... The above example does not factor daylight savings and leap years.
🌐
TecAdmin
tecadmin.net › calculating-days-between-dates-in-java
Calculating Days Between Two Dates in Java – TecAdmin
April 26, 2025 - Calculating Days Between: The ChronoUnit.DAYS.between(startDate, endDate) method calculates the total number of days between the two LocalDate instances. It considers leap years, ensuring accuracy across any span of years. Output: Finally, the program prints the calculated number of days between ...
🌐
Baeldung
baeldung.com › home › java › java dates › how to get all dates between two dates?
How to Get All Dates Between Two Dates? | Baeldung
January 8, 2024 - Notice how, first, we can get the difference of days between two dates using the between function – associated with DAYS constant of ChronoUnit enumeration. Then we create a Stream of integers representing the number of days since the starting ...
🌐
w3resource
w3resource.com › java-exercises › datetime › java-datetime-exercise-38.php
Java - Calculate the difference between two dates in days
May 19, 2025 - Write a Java program to compute the day difference between two dates using LocalDate and ChronoUnit.
🌐
Blogger
javarevisited.blogspot.com › 2015 › 07 › how-to-find-number-of-days-between-two-dates-in-java.html
How to Calculate Difference between two Dates in Java (In Days) - Example Tutorial
In order to solve this problem using joda-time, we need to use a class called LocalDate to represent your dates. It is similar to the LocalDate class of Java 8, or should I say LocalDate is inspired by this class.