Difference between two LocalDateTIme objects in days

I need to find difference between current dateTime LocalDateTIme.now() and another LocaDateTime object called issueDateTime in number of days. Also days to be counted starting from the time in issueDateTime. That is, if issueDateTime is 12/04/2014 5:00 pm days should be counted as per 5 pm and not 12 am. I want to find the difference between issue date and current date in days and then multiply it with rentalTariff/day to return amount from method amountPayable(). thanks

public class RentOut {

private int rentalTariff ;
private LocalDateTime dateTime;
private String rentedTo;
private LocalDateTime returnDateTime;

public RentOut(int rentalTariff) {
    this.rentalTariff = rentalTariff;
}
public void RentIt(LocalDateTime dateTime, String rentedTo) {
    this.dateTime = dateTime;
    this.rentedTo = rentedTo;
    this.returnDateTime = dateTime.plusHours(24);
}
public LocalDateTime getRentDateTime() {
    return dateTime;
}
public LocalDateTime returnDateTime() {
    return returnDateTime;
}
public String getCustomerName() {
    return rentedTo;
}
public int amountPayable() {
  //return LocalDateTime.now().minus(dateTime)*rentalTariff;
}
}
Jon Skeet
people
quotationmark

You can use ChronoUnit.DAYS for this. Compare the LocalTime values to see whether or not the start should be consider to be in "the next day":

import java.time.*;
import java.time.temporal.*;

class Test {
    public static void main(String[] args) {
        // Start time before end time
        showDiff(2014, 9, 25, 15, 0,
                 2014, 9, 27, 17, 0);
        // Start time equal to end time
        showDiff(2014, 9, 25, 17, 0,
                 2014, 9, 27, 17, 0);
        // Start time later than end time
        showDiff(2014, 9, 25, 18, 0,
                 2014, 9, 27, 17, 0);
    }

    public static void showDiff(
        int startYear, int startMonth, int startDay,
        int startHour, int startMinute,
        int endYear, int endMonth, int endDay,
        int endHour, int endMinute) {
        LocalDateTime start = LocalDateTime.of(
            startYear, startMonth, startDay, startHour, startMinute);
        LocalDateTime end = LocalDateTime.of(
            endYear, endMonth, endDay, endHour, endMinute);

        System.out.printf("%s to %s is %d day(s)%n", start, end,
            differenceInDays(start, end));
    }

    public static int differenceInDays(LocalDateTime start, LocalDateTime end) {
        LocalDate startDate = start.toLocalDate();
        LocalDate endDate = end.toLocalDate();
        if (start.toLocalTime().isAfter(end.toLocalTime())) {
            startDate = startDate.plusDays(1);
        }
        return (int) ChronoUnit.DAYS.between(startDate, endDate);
    }
}

Output:

2014-09-25T15:00 to 2014-09-27T17:00 is 2 day(s)
2014-09-25T17:00 to 2014-09-27T17:00 is 2 day(s)
2014-09-25T18:00 to 2014-09-27T17:00 is 1 day(s)

people

See more on this question at Stackoverflow