Reputation: 4991
I am reading about the great new java.time API in Java 8
Something like this
public final class DaysWithBaby
{
private final LocalDate sheSayYes = LocalDate.of(2008,Month.APRIL,26);
private final LocalDate today = LocalDate.now().plusDays(1);
public static void main(String[] args)
{
final DaysWithBaby clazz = new DaysWithBaby();
System.out.println(Period.between(clazz.sheSayYes,clazz.today).getDays());
System.out.println(ChronoUnit.DAYS.between(clazz.sheSayYes,clazz.today));
}
}
Both outputs are listed below.
1
2467
I think this result is correct
System.out.println(ChronoUnit.DAYS.between(clazz.sheSayYes,clazz.today));
But what about this result returning 1
System.out.println(Period.between(clazz.sheSayYes,clazz.today).getDays());
What i am doing wrong any help is hugely appreciate.
Upvotes: 0
Views: 94
Reputation: 201467
The class Period
represents a number of years, months and days (in that order); if you want to get just the days it is up to you to convert it.
private static final LocalDate sheSayYes = LocalDate.of(2008, Month.APRIL,
26);
private static final LocalDate today = LocalDate.now();
public static void main(String[] args) {
Period p = Period.between(sheSayYes, today);
System.out.printf("%d years, %d months, %d days%n", p.getYears(),
p.getMonths(), p.getDays());
System.out.println(ChronoUnit.DAYS.between(sheSayYes, today));
}
Output is
6 years, 9 months, 0 days
2466
And 0 days is correct because it is the 26th today.
Upvotes: 7
Reputation: 3437
Period.getDays, returns the number of days component of the period.
Ie. java.time.Period is an amount of time measured in years, months and days. Period.getDays returns the number of days part of this.
Upvotes: 3