java - How To Calculate The Number Of Days In A Period -
for following period
calculation:
period.between(localdate.of(2015,8,1), localdate.of(2015,9,2))
the result is:
p1m1d
this equivalent 31 days + 1 day = 32 days.
for period
:
period.between(localdate.of(2015,8,1), localdate.of(2015,10,2))
the result :
p2m1d
this equivalent to: 31 days (in august) + 30 days (in september) + 1 (in october) = 62 days
is there method in java.time
package give number of days in period
? can't find one. not sure if have overlooked or if plain not there.
from documentation:
to define amount of time date-based values (years, months, days), use
period
class.period
class provides various methods, suchgetmonths
,getdays
, ,getyears
.to present amount >of time measured in single unit of time, such days, can usechronounit.between
method.localdate today = localdate.now(); localdate birthday = localdate.of(1960, month.january, 1); period p = period.between(birthday, today); long p2 = chronounit.days.between(birthday, today); system.out.println("you " + p.getyears() + " years, " + p.getmonths() + " months, , " + p.getdays() + " days old. (" + p2 + " days total)");
the code produces output similar following:
you 53 years, 4 months, , 29 days old. (19508 days total)
Comments
Post a Comment