-2

I'm trying to calculate the difference between two dates in java. For example, I have:

LocalDate TodayDate = LocalDate.now(); //That returns me ==> year/month/day
LocalDate ExpireDay = LocalDate.of("2018-01-12");

I want to know how many days are between the two dates, how can I do? I tried using ExpireDay.minus(Period.ofDays()); but it doesn't work... :(

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • 1
    https://stackoverflow.com/questions/27005861/calculate-days-between-two-dates-in-java-8 might be helpful. –  Oct 22 '17 at 17:44
  • It already answered [enter link description here](https://stackoverflow.com/questions/17940200/how-to-find-the-duration-of-difference-between-two-dates-in-java) – Sounak Saha Oct 22 '17 at 17:44
  • Assuming it's Java 8 (although the `LocalDate` class has no `off` method): https://stackoverflow.com/a/24163958 –  Oct 22 '17 at 17:45
  • 1
    `ExpireDay.minus(Period.ofDays())` can't possibly work; it doesn't even reference `TodayDate`. In general, don't just throw code at the compiler and see what sticks. If you don't understand your code, it's liable to fail in circumstances you didn't anticipate. – shmosel Oct 22 '17 at 18:00
  • 1
    Please make a search before asking. A simple Google for *"get days between dates java"* gives lots of results, many of them in Stack Overflow (including the ones linked above) - see the [ask] page and also [this page](https://meta.stackoverflow.com/q/261592). Please also include code that compiles, so we can properly test instead of guessing what you meant - see how to provide a [mcve]. –  Oct 22 '17 at 18:12

2 Answers2

6

For a total number of days:

long days = ChronoUnit.DAYS.between( start , stop ) ;

For a number of years, months, and days:

Period p = Period.between( start , stop ) ;
int y = p.getYears() ;
int m = p.getMonths() ;
int d = p.getDays() ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
2

You can find days between two dates using localdate class by

LocalDate today = LocalDate.now();
LocalDate ExpireDay = LocalDate.of(2018, Month.JANUARY, 12);
long daysBetween = DAYS.between(today, ExpireDay);
System.out.println(daysBetween);