w3resource

Java DateTime, Calendar Exercises: Calculate the difference between two dates in days

Java DateTime, Calendar: Exercise-38 with Solution

Write a Java program to calculate the difference between two dates in days.

Sample Solution:

Java Code:

//MIT License: https://bit.ly/35gZLa3
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;

public class Main {

    public static void main(String[] args) {

        System.out.println("\nBefore JDK 8:");

        Calendar cal1 = Calendar.getInstance();
        cal1.set(2019, 0, 1);
        Calendar cal2 = Calendar.getInstance();
        cal2.set(2020, 2, 1);

        System.out.println("\nDate/Calendar case: " + cal1.getTime() + " <-> " + cal2.getTime());

        long inMs = Math.abs(cal1.getTimeInMillis() - cal2.getTimeInMillis());
        long inDays = Math.abs(TimeUnit.DAYS.convert(inMs, TimeUnit.MILLISECONDS));

        System.out.println("Difference in milliseconds is: " + inMs);
        System.out.println("Difference in days is: " + inDays);

        System.out.println("\nStarting with JDK 8:");

        LocalDate ld1 = LocalDate.of(2019, 1, 1);
        LocalDate ld2 = LocalDate.of(2020, 3, 1);

        System.out.println("\nLocalDate case: " + ld1 + " <-> " + ld2);

        long between_In_Days = Math.abs(ChronoUnit.DAYS.between(ld1, ld2));
        long between_In_Months = Math.abs(ChronoUnit.MONTHS.between(ld1, ld2));
        long between_In_Years = Math.abs(ChronoUnit.YEARS.between(ld1, ld2));
        long until_In_Days = Math.abs(ld1.until(ld2, ChronoUnit.DAYS));
        long until_In_Months = Math.abs(ld1.until(ld2, ChronoUnit.MONTHS));
        long until_In_Years = Math.abs(ld1.until(ld2, ChronoUnit.YEARS));
        Period period = ld1.until(ld2);
        System.out.println("Difference as Period: "
                + period.getYears() + "y" + period.getMonths() + "m" + period.getDays() + "d");
        System.out.println("Difference in days is via between(): " + between_In_Days);
        System.out.println("Difference in months is via between(): " + between_In_Months);
        System.out.println("Difference in years is via between(): " + between_In_Years);
        System.out.println("Difference in days is via until(): " + until_In_Days);
        System.out.println("Difference in months is via until(): " + until_In_Months);
        System.out.println("Difference in years is via until(): " + until_In_Years);

        LocalDateTime ldt1 = LocalDateTime.of(2019, 1, 1, 22, 15, 15);
        LocalDateTime ldt2 = LocalDateTime.of(2020, 1, 1, 23, 15, 15);

        System.out.println("\nLocalDateTime case: " + ldt1 + " <-> " + ldt2);

        long betweenInMinutesWithoutZone = Math.abs(ChronoUnit.MINUTES.between(ldt1, ldt2));
        long untilInMinutesWithoutZone = Math.abs(ldt1.until(ldt2, ChronoUnit.HOURS));
        System.out.println("Difference in minutes without zone: " + betweenInMinutesWithoutZone);
        System.out.println("Difference in hours without zone: " + untilInMinutesWithoutZone);

        System.out.println("\nZonedDateTime case:");

        ZonedDateTime zdt1 = ldt1.atZone(ZoneId.of("Europe/Bucharest"));
        ZonedDateTime zdt2 = zdt1.withZoneSameInstant(ZoneId.of("Australia/Perth")).plusHours(1);
        ZonedDateTime zdt3 = ldt2.atZone(ZoneId.of("Australia/Perth"));

        long betweenInMinutesWithZone12 = Math.abs(ChronoUnit.MINUTES.between(zdt1, zdt2));
        long untilInHoursWithZone12 = Math.abs(zdt1.until(zdt2, ChronoUnit.HOURS));
        long betweenInMinutesWithZone13 = Math.abs(ChronoUnit.MINUTES.between(zdt1, zdt3));
        long untilInHoursWithZone13 = Math.abs(zdt1.until(zdt3, ChronoUnit.HOURS));

        System.out.println("Europe/Bucharest: " + zdt1 + " <-> Australia/Perth: " + zdt2);
        System.out.println("Difference in minutes with zone (same instant): " + betweenInMinutesWithZone12);
        System.out.println("Difference in hours with zone (same instant): " + untilInHoursWithZone12);

        System.out.println("\nEurope/Bucharest: " + zdt1 + " <-> Australia/Perth: " + zdt3);
        System.out.println("Difference in minutes with zone (not same instant): " + betweenInMinutesWithZone13);
        System.out.println("Difference in hours with zone: " + untilInHoursWithZone13);
    }
} 

Sample Output:

Before JDK 8:

Date/Calendar case: Tue Jan 01 07:15:13 UTC 2019 <-> Sun Mar 01 07:15:13 UTC 2020
Difference in milliseconds is: 36720000012
Difference in days is: 425

Starting with JDK 8:

LocalDate case: 2019-01-01 <-> 2020-03-01
Difference as Period: 1y2m0d
Difference in days is via between(): 425
Difference in months is via between(): 14
Difference in years is via between(): 1
Difference in days is via until(): 425
Difference in months is via until(): 14
Difference in years is via until(): 1

LocalDateTime case: 2019-01-01T22:15:15 <-> 2020-01-01T23:15:15
Difference in minutes without zone: 525660
Difference in hours without zone: 8761

ZonedDateTime case:
Europe/Bucharest: 2019-01-01T22:15:15+02:00[Europe/Bucharest] <-> Australia/Perth: 2019-01-02T05:15:15+08:00[Australia/Perth]
Difference in minutes with zone (same instant): 60
Difference in hours with zone (same instant): 1

Europe/Bucharest: 2019-01-01T22:15:15+02:00[Europe/Bucharest] <-> Australia/Perth: 2020-01-01T23:15:15+08:00[Australia/Perth]
Difference in minutes with zone (not same instant): 525300
Difference in hours with zone: 8755

Flowchart:

Flowchart: Java DateTime, Calendar Exercises - Get seconds since 1970

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous:Write a Java program to get seconds since 1970.
Next: Write a java program to convert String to date and time and vice a versa.

What is the difficulty level of this exercise?



Java: Tips of the Day

Easiest way to convert a List to a Set in Java:

Set<Foo> foo = new HashSet<Foo>(myList);

Ref: https://bit.ly/3qIJSTZ