# How to calculate days between two dates in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jan 10, 2020, updated Nov 16, 2022. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-date-difference

> A complete guide to calculating the days, hours, and minutes between two dates in Java using the new date and time API.

In my [previous article](https://attacomsian.com/blog/java-add-days-to-date), I wrote about adding days to an instance of a date in Java. In this article, you will learn how to calculate the duration between two dates using [Java 8 new date and time API](https://attacomsian.com/blog/java-new-date-time-api) and the legacy `Date` API.

## Java 8 Date & Time API

Java 8 introduced a whole new date and time API (classes in the `java.time.*` package) to fix the shortcomings of the legacy `Date` and `Calendar` API. The new API provides several utility methods for date and time manipulations. You can use utility classes like `Period`, `Duration`, and `ChronoUnit` to calculate the difference between two instances of the new date and time API class.

### Days between two `LocalDate` objects

The `LocalDate` class **presents the date without time** in ISO-8601 format (`yyyy-MM-dd`). Unlike the legacy `Date` class, it doesn't store any timezone information.

The following example demonstrates how you can use the [`Duration`](https://attacomsian.com/blog/java-new-date-time-api#duration-class) class to calculate the number of days between two `LocalDate` instances:

```java
// create date instances
LocalDate localDate1 = LocalDate.parse("2019-12-31");
LocalDate localDate2 = LocalDate.parse("2020-01-08");

// calculate difference
long days = Duration.between(localDate1.atStartOfDay(), localDate2.atStartOfDay()).toDays();

// print days
System.out.println("Days between " + localDate1 + " and " + localDate2 + ": " + days);
```

Here is the output of the above code snippet:

```bash
Days between 2019-12-31 and 2020-01-08: 8
```

The new date and time API also provides [`ChronoUnit`](https://attacomsian.com/blog/java-new-date-time-api#chrono-units-enum) enum class to represent individual date-time units such as days, months, years, hours, seconds, etc. 

Each unit provides an implementation for a method called `between()` to calculate the amount of time between two temporal objects for that specific unit.

Let us look at the below example that calculates the days and months between two `LocalDate` instances using `ChronoUnit`:

```java
// create date instances
LocalDate pastDate = LocalDate.of(2019, Month.AUGUST, 15);
LocalDate now = LocalDate.now();

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDate, now);
long days = ChronoUnit.DAYS.between(pastDate, now);

// print days & months
System.out.println("Months between " + pastDate + " and " + now + ": " + months);
System.out.println("Days between " + pastDate + " and " + now + ": " + days);
```

The above code snippet will output the following:

```bash
Months between 2019-08-15 and 2020-01-10: 4
Days between 2019-08-15 and 2020-01-10: 148
```

### Hours between two `LocalTime` objects

A `LocalTime` **represents time without date** in ISO-8601 format (`HH:mm:ss.SSS`). To calculate the difference between two instances of `LocalTime`, you need to use the [`Duration`](https://attacomsian.com/blog/java-new-date-time-api#duration-class) class that represents the duration between two times.

Here is an example:

```java
// create time instances
LocalTime firstTime = LocalTime.of(11, 30);
LocalTime secondTime = LocalTime.of(22, 35);

// calculate the difference between times
Duration duration = Duration.between(firstTime, secondTime);

// print the difference
System.out.println("Hours between " + firstTime + " and " + secondTime + ": " + duration.toHours());
System.out.println("Minutes between " + firstTime + " and " + secondTime + ": " + duration.toMinutes());
```

The above example will print the following on the console:

```bash
Hours between 11:30 and 22:35: 11
Minutes between 11:30 and 22:35: 665
```

Alternatively, you can also use `ChronoUnit` to **calculate the difference** between two `LocalDate` in terms of **specific time units** as shown below:

```java
// create time instances
LocalTime firstTime = LocalTime.of(10, 15, 45);
LocalTime secondTime = LocalTime.of(14, 50, 15);

// calculate the difference between times
long hours = ChronoUnit.HOURS.between(firstTime, secondTime);
long minutes = ChronoUnit.MINUTES.between(firstTime, secondTime);
long seconds = ChronoUnit.SECONDS.between(firstTime, secondTime);

// print the difference
System.out.println("Hours between " + firstTime + " and " + secondTime + ": " + hours);
System.out.println("Minutes between " + firstTime + " and " + secondTime + ": " + minutes);
System.out.println("Seconds between " + firstTime + " and " + secondTime + ": " + seconds);
```

Here is what the output looks like:

```bash
Hours between 10:15:45 and 14:50:15: 4
Minutes between 10:15:45 and 14:50:15: 274
Seconds between 10:15:45 and 14:50:15: 16470
```

### Days between two `LocalDateTime` objects

The `LocalDateTime` class **represents both date and time without timezone** in ISO-8601 format (`yyyy-MM-ddTHH:mm:ss.SSS`). The following example demonstrates how you can calculate the difference between two `LocalDateTime` instances of terms of days using `Duration`:

```java
// create date time instances
LocalDateTime dateTime1 = LocalDateTime.parse("2018-08-02T15:14");
LocalDateTime dateTime2 = LocalDateTime.parse("2019-02-14T12:45");

// calculate difference
Duration duration = Duration.between(dateTime1, dateTime2);

// print the difference
System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays());
System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours());
```

The above example will output the following:

```bash
Days between 2018-08-02T15:14 and 2019-02-14T12:45: 195
Hours between 2018-08-02T15:14 and 2019-02-14T12:45: 4701
```

Let us look at another example that **uses `ChronoUnit` to find out the difference** between two `LocalDateTime` objects:

```java
// create date time instances
LocalDateTime pastDateTime = LocalDateTime.of(2019, Month.NOVEMBER, 12, 10, 40);
LocalDateTime now = LocalDateTime.now();

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDateTime, now);
long days = ChronoUnit.DAYS.between(pastDateTime, now);

// print days & months
System.out.println("Months between " + pastDateTime + " and " + now + ": " + months);
System.out.println("Days between " + pastDateTime + " and " + now + ": " + days);
```

You should see the following output when you execute the above example:

```bash
Months between 2019-11-12T10:40 and 2020-01-10T18:36:48.356: 1
Days between 2019-11-12T10:40 and 2020-01-10T18:36:48.356: 59
```

### Days between two `ZonedDateTime` objects

A `ZonedDateTime` **represents a date and time with a timezone** in ISO-8601 format (e.g `2019-05-15T10:15:30+01:00[Europe/Paris]`). It deals with timezone-specific dates and times in Java 8 and higher.

Here is an example that shows how you can measure the difference between two `ZonedDateTime` instances:

```java
// create date time instances
ZonedDateTime dateTime1 = ZonedDateTime.parse("2019-05-15T10:15:30+01:00[Europe/Paris]");
ZonedDateTime dateTime2 = ZonedDateTime.parse("2020-01-05T12:00:33+05:00[Asia/Karachi]");

// calculate difference
Duration duration = Duration.between(dateTime1, dateTime2);

// print the difference
System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays());
System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours());
```

As you can see above, we have created two `ZonedDateTime` instances in different time zones. The great thing about the new date and time API is that it will automatically calculate the timezone differences, as shown in the below output:

```bash
Days between 2019-05-15T10:15:30+02:00[Europe/Paris] and 2020-01-05T12:00:33+05:00[Asia/Karachi]: 234
Hours between 2019-05-15T10:15:30+02:00[Europe/Paris] and 2020-01-05T12:00:33+05:00[Asia/Karachi]: 5638
```

The same date and time difference calculations can be performed through the `ChronoUnit` enum class as shown below:

```java
// create date time instances
ZonedDateTime pastDateTime = ZonedDateTime.of(2018,2, 18, 10, 30, 0, 0, ZoneId.of("Europe/Paris"));
ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault());

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDateTime, now);
long days = ChronoUnit.DAYS.between(pastDateTime, now);

// print days & months
System.out.println("Months between " + pastDateTime + " and " + now + ": " + months);
System.out.println("Days between " + pastDateTime + " and " + now + ": " + days);
```

Here is the output for the above example:

```bash
Months between 2018-02-18T10:30+01:00[Europe/Paris] and 2020-01-10T18:48:38.368+05:00[Asia/Karachi]: 22
Days between 2018-02-18T10:30+01:00[Europe/Paris] and 2020-01-10T18:48:38.368+05:00[Asia/Karachi]: 691
```

### Days between two `OffsetDateTime` objects

Finally, the last new date and time API class we'll discuss is `OffsetDateTime`. It **represents a date and time with an offset from UTC/Greenwich** in the ISO-8601 format (e.g., `2018-12-30T23:15:30+03:30`).

The following example shows how you can measure the difference between two `OffsetDateTime` instances:

```java
// create date time instances
OffsetDateTime dateTime1 = OffsetDateTime.parse("2018-12-30T23:15:30+03:30");
OffsetDateTime dateTime2 = OffsetDateTime.parse("2019-07-17T05:15:30-05:00");

// calculate difference
Duration duration = Duration.between(dateTime1, dateTime2);

// print the difference
System.out.println("Days between " + dateTime1 + " and " + dateTime2 + ": " + duration.toDays());
System.out.println("Hours between " + dateTime1 + " and " + dateTime2 + ": " + duration.toHours());
```

The above example will output the following:

```bash
Days between 2018-12-30T23:15:30+03:30 and 2019-07-17T05:15:30-05:00: 198
Hours between 2018-12-30T23:15:30+03:30 and 2019-07-17T05:15:30-05:00: 4766
```

Another way to find out the difference between two `OffsetDateTime` is using the `ChronoUnit` enum class:

```java
// create date time instances
OffsetDateTime pastDateTime = OffsetDateTime.of(2019, 8, 25, 15, 40, 0, 0, ZoneOffset.of("+03:00"));
OffsetDateTime now = OffsetDateTime.now(ZoneOffset.systemDefault());

// calculate difference
long months = ChronoUnit.MONTHS.between(pastDateTime, now);
long days = ChronoUnit.DAYS.between(pastDateTime, now);

// print days & months
System.out.println("Months between " + pastDateTime + " and " + now + ": " + months);
System.out.println("Days between " + pastDateTime + " and " + now + ": " + days);
```

Here is the output of the above code snippet:

```bash
Months between 2019-08-25T15:40+03:00 and 2020-01-10T18:56:30.484+05:00: 4
Days between 2019-08-25T15:40+03:00 and 2020-01-10T18:56:30.484+05:00: 138
```

> Check out [Introduction to Java 8 Date and Time API](https://attacomsian.com/blog/java-new-date-time-api) tutorial for more new date and time API examples.

## Days between two `Date` & `Calendar` objects

Before Java 8, `java.util.Date` and `java.util.Calendar` classes were used for handling dates and times. These old classes have many flaws that were fixed with the release of the new date and time API in Java 8. Since these classes are still actively used in old projects, it is worth knowing how to calculate the difference between two `Date` objects.

The following example demonstrates how you can [parse strings](https://attacomsian.com/blog/java-convert-string-to-date) to create two instances of `Date` and then find the difference between them in milliseconds. Later, we convert the milliseconds into days and print the result on the console:

```java
// date pattern
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

// parse string to date
Date dateBefore = formatter.parse("2019-12-05");
Date dateAfter = formatter.parse("2020-01-05");

// calculate difference in milliseconds
long ms = Math.abs(dateAfter.getTime() - dateBefore.getTime());

// convert milliseconds to days
long days = TimeUnit.DAYS.convert(ms, TimeUnit.MILLISECONDS);

// print days
System.out.println("Days Between Dates: " + days);
```

The above program will output the following:

```bash
Days Between Dates: 31
```

## Conclusion

In this article, we looked at different ways to calculate the difference between two dates in Java. We discussed Java 8 new date and time API classes that provide utility methods to calculate the number of days between the given date and time instances.

The [new date and time API](https://attacomsian.com/blog/java-new-date-time-api) introduced in Java 8 provides a wide range of classes that have simplified working with dates and times.

**Read next:** [How to get current date and time in Java](https://attacomsian.com/blog/java-get-current-date-time)
