HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java 8 – Calculate days between two dates

Java 8 – Calculate days between two dates

Learn to calculate number of days between two dates in Java 8 using ChronoUnit.DAYS.between() and LocalDate.until() methods.

Read More : Calculate business days between two dates

1. ChronoUnit.DAYS.between()

This is the simplest of all solutions. Internally, it is as simple as date1.toEpochDay() - date2.toEpochDay(). It count the days since year zero till both LocalDate instances and subtract it.

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DaysBetweenDates 
{
	public static void main(String[] args) 
	{
		LocalDate date1 = LocalDate.now();
		LocalDate date2 = date1.plusDays(99);
		
		long diffInDays = ChronoUnit.DAYS.between(date1, date2);
		
		System.out.println(diffInDays);  // 99
	}
}

Program output.

99

2. LocalDate.until()

This solution is very much similar to previous one. And internally, it also uses the same technique as above i.e. date1.toEpochDay() - date2.toEpochDay().

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;

public class DaysBetweenDates 
{
	public static void main(String[] args) 
	{
		LocalDate date1 = LocalDate.now();
		LocalDate date2 = date1.plusDays(99);
		
		long diffInDays = date1.until(date2, ChronoUnit.DAYS);
		
		System.out.println(diffInDays);  // 99
	}
}

Program output.

99

Drop me your questions related to calculating days between two dates in Java.

Happy Learning !!

Sourcecode Download

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java Date Time Tutorial

  • Java – Date Time APIs
  • Java – Date Parsing
  • Java – Date Formatting
  • Java 8 – LocalDate
  • Java 8 – LocalTime
  • Java 8 – LocalDateTime
  • Java 8 – ZonedDateTime
  • Java 8 – Period
  • Java 8 – DateTimeFormatter
  • Java 8 – TemporalAdjusters
  • Java 8 – TemporalQuery
  • Java 8 – DayOfWeek
  • Java – Date
  • Java – Locale

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces