HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Date Time / Java 8 Difference Between Two Dates

Java 8 Difference Between Two Dates

Java examples to calculate the difference between two dates in Java 8. First we will learn to calculate the difference using Jodatime API which was available even before Java 8 release. If you are still not using Java 8, then JodaTime should be your first choice.

Later 3 methods are using new date time API features available since Java 8.

1. JodaTime – Difference between two dates [Before Java 8]

Maven

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.10</version>
</dependency>

Example

As you all prefer readability, I will suggest to use Jodatime library (which actually inspired Java 8 date/time APIs). e.g.

public void difference_between_two_dates_duration_Joda()
 {
     DateTime dateOfBirth = new DateTime(1988, 7, 4, 0, 0, GregorianChronology.getInstance());
     DateTime currentDate = new DateTime();
     Days diffInDays = Days.daysBetween(dateOfBirth, currentDate);
     Hours diffInHours = Hours.hoursBetween(dateOfBirth, currentDate);
     Minutes diffInMinutes = Minutes.minutesBetween(dateOfBirth, currentDate);
     Seconds seconds = Seconds.secondsBetween(dateOfBirth, currentDate);
 }

2. Java 8 – Difference between two dates

Java Dates have always been lacking enough support to express date and time periods in effective way. Java 8 made first attempt to upgrade this date/time API. If you are using java 8 in your project, then definitely use either of below given ways to calculate date/time differences between two dates.

2.1. java.time.Period example to know the difference in days/months/years

Java program to get difference between two dates in days using Period class.

LocalDate endofCentury = LocalDate.of(2014, 01, 01);
LocalDate now = LocalDate.now();

Period diff = Period.between(endofCentury, now);

System.out.printf("Difference is %d years, %d months and %d days old", 
					diff.getYears(), diff.getMonths(), diff.getDays());

2.2. java.time.temporal.ChronoUnit example to know the difference in days/months/years

Java program to get difference between two dates in months using ChronoUnit class.

public void difference_between_two_dates_java8()
{
     LocalDate dateOfBirth = LocalDate.of(1980, Month.JULY, 4);
     LocalDate currentDate = LocalDate.now();
     long diffInDays = ChronoUnit.DAYS.between(dateOfBirth, currentDate);
     long diffInMonths = ChronoUnit.MONTHS.between(dateOfBirth, currentDate);
     long diffInYears = ChronoUnit.YEARS.between(dateOfBirth, currentDate);
}

You can use ChronoUnit to know the difference in smaller time units e.g. milliseconds, minutes etc. But in that case, you will have to use LocalDateTime in place of LocalDate as used in first example.

public void difference_between_two_dates_duration()
 {
     LocalDateTime dateTime = LocalDateTime.of(1988, 7, 4, 0, 0);
     LocalDateTime dateTime2 = LocalDateTime.now();
     long diffInNano = ChronoUnit.NANOS.between(dateTime, dateTime2);
     long diffInSeconds = ChronoUnit.SECONDS.between(dateTime, dateTime2);
     long diffInMilli = ChronoUnit.MILLIS.between(dateTime, dateTime2);
     long diffInMinutes = ChronoUnit.MINUTES.between(dateTime, dateTime2);
     long diffInHours = ChronoUnit.HOURS.between(dateTime, dateTime2);
 }

2.3. java.time.Duration example to know the difference in millis/seconds/minutes etc

Java program to get difference between two dates in millis using Duration class.

public void difference_between_two_dates_duration()
 {
     LocalDateTime dateTime = LocalDateTime.of(1988, 7, 4, 0, 0);
     LocalDateTime dateTime2 = LocalDateTime.now();
    int diffInNano = java.time.Duration.between(dateTime, dateTime2).getNano();
     long diffInSeconds = java.time.Duration.between(dateTime, dateTime2).getSeconds();
     long diffInMilli = java.time.Duration.between(dateTime, dateTime2).toMillis();
     long diffInMinutes = java.time.Duration.between(dateTime, dateTime2).toMinutes();
     long diffInHours = java.time.Duration.between(dateTime, dateTime2).toHours();
 }

Above all 3 ways have enough readability as well as flexibility to know the difference between date in multiple time units. Feel free to use above programs to usecase such as calculate number of days between two dates in java.

Happy Learning !!

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.

Feedback, Discussion and Comments

  1. carlos

    January 12, 2017

    How to return DateTime or LocalDate in this diferrence(dates)

  2. Savani

    April 12, 2016

    This is exactly I was looking to developed. Many Thank you ! Great Post ! keep it up !

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