Calculate date/time difference in java

To calculate the difference between two dates in Java, you can use the java.time package (part of Java 8 and later) or the java.util.Calendar class (part of the older java.util package).

Here is an example of how to use the java.time package to calculate the difference between two dates:

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

public class DateTimeDifferenceExample {

    public static void main(String[] args) {
        LocalDateTime start = LocalDateTime.of(2020, 1, 1, 0, 0);
        LocalDateTime end = LocalDateTime.of(2020, 1, 2, 0, 0);
        Duration duration = Duration.between(start, end);
        System.out.println("Duration: " + duration.toDays() + " days");
    }

}

This example calculates the difference between two dates, start and end, and prints the result in terms of the number of days.

Here is an example of how to use the java.util.Calendar class to calculate the difference between two dates:

import java.util.Calendar;
import java.util.Date;

public class DateTimeDifferenceExample {

    public static void main(String[] args) {
        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();
        start.set(2020, 0, 1);
        end.set(2020, 0, 2);
        long diff = end.getTimeInMillis() - start.getTimeInMillis();
        System.out.println("Duration: " + diff / (24 * 60 * 60 * 1000) + " days");
    }

}

This example calculates the difference between two dates, start and end, and prints the result in terms of the number of days.

I hope this helps. Let me know if you have any questions.