Main Tutorials

Java 8 – Difference between two LocalDate or LocalDateTime

In Java 8, we can use Period, Duration or ChronoUnit to calculate the difference between two LocalDate or LocaldateTime.

  1. Period to calculate the difference between two LocalDate.
  2. Duration to calculate the difference between two LocalDateTime.
  3. ChronoUnit for everything.

1. Period

JavaLocalDate.java

package com.mkyong.java8;

import java.time.LocalDate;
import java.time.Period;

public class JavaLocalDate {

    public static void main(String[] args) {

        LocalDate from = LocalDate.of(2020, 5, 4);
        LocalDate to = LocalDate.of(2020, 10, 10);

        Period period = Period.between(from, to);

        System.out.print(period.getYears() + " years,");
        System.out.print(period.getMonths() + " months,");
        System.out.print(period.getDays() + " days");

    }

}

Output


0 years,5 months,6 days

2. Duration

JavaLocalDateTime.java

package com.mkyong.java8;

import java.time.Duration;
import java.time.LocalDateTime;

public class JavaLocalDateTime {

    public static void main(String[] args) {

        LocalDateTime from = LocalDateTime.of(2020, 10, 4,
                10, 20, 55);
        LocalDateTime to = LocalDateTime.of(2020, 10, 10,
                10, 21, 1);

        Duration duration = Duration.between(from, to);

        // days between from and to
        System.out.println(duration.toDays() + " days");

        // hours between from and to
        System.out.println(duration.toHours() + " hours");

        // minutes between from and to
        System.out.println(duration.toMinutes() + " minutes");

        // seconds between from and to
        System.out.println(duration.toSeconds() + " seconds");
        System.out.println(duration.getSeconds() + " seconds");

    }

}

Output


6 days
144 hours
8640 minutes
518406 seconds
518406 seconds

3. ChronoUnit

JavaChronoUnit.java

package com.mkyong.java8;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

public class JavaChronoUnit {

    public static void main(String[] args) {

        LocalDateTime from = LocalDateTime.of(2020, 10, 4,
                10, 20, 55);
        LocalDateTime to = LocalDateTime.of(2020, 11, 10,
                10, 21, 1);

        long years = ChronoUnit.YEARS.between(from, to);
        long months = ChronoUnit.MONTHS.between(from, to);
        long weeks = ChronoUnit.WEEKS.between(from, to);
        long days = ChronoUnit.DAYS.between(from, to);
        long hours = ChronoUnit.HOURS.between(from, to);
        long minutes = ChronoUnit.MINUTES.between(from, to);
        long seconds = ChronoUnit.SECONDS.between(from, to);
        long milliseconds = ChronoUnit.MILLIS.between(from, to);
        long nano = ChronoUnit.NANOS.between(from, to);

        System.out.println(years + " years");
        System.out.println(months + " months");
        System.out.println(weeks + " weeks");
        System.out.println(days + " days");
        System.out.println(hours + " hours");
        System.out.println(minutes + " minutes");
        System.out.println(seconds + " seconds");
        System.out.println(milliseconds + " milliseconds");
        System.out.println(nano + " nano");

    }

}

Output


0 years
1 months
5 weeks
37 days
888 hours
53280 minutes
3196806 seconds
3196806000 milliseconds
3196806000000000 nano

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Leandro
4 years ago

How is it possible, if the methods between receive Temporal type?