1

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;
}
}
Meena Chaudhary
  • 9,909
  • 16
  • 60
  • 94
  • 2
    Please show a short but complete example demonstrating the problem - just a simple console app with two `LocalDateTime` values, and how you're trying to solve the issue at the moment. – Jon Skeet Sep 25 '14 at 10:51
  • code added, have a look. – Meena Chaudhary Sep 25 '14 at 11:00
  • 1
    That's not a short but complete example, is it? It doesn't contain any sample data, and it has a bunch of unnecessary bits. A short but complete example in this case is likely to be a class containing just a `main` method with about three lines of code in... – Jon Skeet Sep 25 '14 at 11:23
  • 1
    Please clarify: is this Java 8, or Joda Time? – Jon Skeet Sep 25 '14 at 11:43

2 Answers2

3

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)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Use the day of the year from the LocalDateTime to calculate this. Take into account that the original date time could be after the current date if there is a difference in year. (which would result in negative results)

Kurt Du Bois
  • 7,550
  • 4
  • 25
  • 33
  • 1
    Day-of-year is likely to be inappropriate if it can span years... there are simpler approaches. – Jon Skeet Sep 25 '14 at 11:24
  • You could indeed use the Days class from Joda time to calculate the days between both dates as suggested here: http://stackoverflow.com/questions/3299972/difference-in-days-between-two-dates-in-java?rq=1. – Kurt Du Bois Sep 25 '14 at 11:37
  • There's no need to use Joda Time here. `ChronoUnit.DAYS` is the broad equivalent in Java 8. – Jon Skeet Sep 25 '14 at 11:37
  • Now you're taking the assumption that the OP uses Java 8. Since it was not specified I tried to answer in a way that works in most recent Java versions. – Kurt Du Bois Sep 25 '14 at 11:42
  • 1
    Yes, I've assumed that given that I'd expect Joda Time to be explicitly mentioned otherwise. (It's a third party library, vs JDK 8 which is just "the current version of Java.) Will ask for clarification though. – Jon Skeet Sep 25 '14 at 11:43
  • Since the source code provided by the OP already specifies the LocalDateTime I just assumed he forgot to mention that he works with that 3rd party library. – Kurt Du Bois Sep 25 '14 at 11:44
  • 1
    But Java 8 has `LocalDateTime` too. So there's nothing that uniquely specifies it here. – Jon Skeet Sep 25 '14 at 11:46