0

If I do:

public static void main(String[] args) {
    LocalDate d1 = LocalDate.of(2015, Month.MARCH, 12);
    LocalDate d2 = LocalDate.of(2015, Month.APRIL, 13);
    System.out.println(d1.until(d2).getDays());
    // Prints 11
    LocalDate d3 = LocalDate.of(2015, Month.APRIL, 25);
    // Prints 23.

Both of which are incorrect. The second output makes sense as there is 1 month and 23 days between them.

How do I get the total number of days between?

I would want the first output to be 32 Days and the second to be 44 days (the total number of days between the two dates).

What am I doing wrong? I don't see a totalDays() method.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
SwiftySwift
  • 477
  • 4
  • 13

2 Answers2

2

You should probably not use a period (which has year and month components) if you are only interested in days. One solution to your question is:

System.out.println(DAYS.between(d1, d2)); //32

or alternatively:

System.out.println(d1.until(d2, DAYS)); //32

Note: I'm using import static java.time.temporal.ChronoUnit.DAYS;

assylias
  • 321,522
  • 82
  • 660
  • 783
0

I don't know how much you want to do with dates and possible you don't need it but I really recommend Joda Time and here is how to get number of days between two dates.
In Java 8 we finally have good Dates library but if you don't use Java 8 then you have to get Joda Time

Community
  • 1
  • 1
Michu93
  • 5,058
  • 7
  • 47
  • 80