java date difference in days

java

To get the difference between two java.util.Date objects in days, you can use the getTime method of the Date class to get the number of milliseconds since the epoch (midnight, January 1, 1970), and then divide the difference by the number of milliseconds in a day.

Here is an example:

import java.util.Date;

public class Main {
  public static void main(String[] args) {
    Date date1 = new Date();
    Date date2 = new Date(date1.getTime() + 1000L * 60L * 60L * 24L * 10L);
    long diffInMilliseconds = Math.abs(date1.getTime() - date2.getTime());
    long diffInDays = diffInMilliseconds / (1000L * 60L * 60L * 24L);
    System.out.println(diffInDays);
  }
}

This will print the difference between the two dates in days.

Note: This method will not take into account differences in time zones or daylight saving time. If you want to take these into account, you can use the classes of the java.time package, such as LocalDate, Duration, and ChronoUnit.