1

I have Period object which comes from api. I have to calculate total days it contains. I found many answers how to get days between two dates, but no one answers how I can get total days exactly from Period object.

E.g:

LocalDate start = LocalDate.now();
LocalDate end = LocalDate.now().plusYears(1);

Period period = Period.between(start, end);

I have only the last object with name period and i have to get 365 days from it.

getDays() returns only days count within one month. And I don't have two dates objects. Only period.

Squeez
  • 919
  • 2
  • 12
  • 30

2 Answers2

0

You might wanna read up on the Period Java API Documentation. Period represents the time duration in this format, "2 years, 3 months and 4 days" For your case above, its exactly 1 year, 0 Months and 0 days. When you do a get Days, it will show you 0 days.

Try with an end date of LocalDate end = LocalDate.now().minusDays(398); The getYears(), getMonths(), getDays() each returns its own value. ;)

Since you already have a Period object, you can do a function to get the number of days from a Period Object using the getYears, getMonths and getDays() methods then sum them together.

The Duration object might help with that.

Timmay
  • 158
  • 1
  • 2
  • 16
-1

did you tried import java.time package and use Period class? if you didn't, here's a little help for it https://docs.oracle.com/javase/8/docs/api/java/time/Period.html

Dave Butcher
  • 35
  • 10