Search code examples
javadateperiod

Subtracting two date values and returning the different in number of days


Bearing the risk of being redundant, I would like to know how one can subtract 2 date values and store the result which should be in number of days into an integer. I am using Java for this exercise.


Solution

  • If you have two LocalDates, you can use:

    longs days = ChronoUnit.DAYS.between(date1, date2);
    

    Note that Period::getDays does something different: for a period of one year and one day, Period::getDays will return 1, not 366!