pandiaraj
pandiaraj

Reputation: 610

Get the Period between two local date instances like P1Y2M10DT2H30M

How can I obtain the Period between two different LocalDate instances using Java.

I googled, not able to find it.

Upvotes: 0

Views: 86

Answers (2)

Ravindra Ranwala
Ravindra Ranwala

Reputation: 21124

A Period is the amount of time between two LocalDate. Same concept as Duration. Here's the difference between the two: A Duration measures an amount of time using time-based values (seconds, nanoseconds). A Period uses date-based values (years, months, days). However, using Period you can't get the hours, mins as you mentioned in the problem statement above.

Upvotes: 0

pandiaraj
pandiaraj

Reputation: 610

Yes, I got it.

LocalDate firstDate = LocalDate.of(2010, 5, 17); // 2010-05-17
LocalDate secondDate = LocalDate.of(2015, 3, 7); // 2015-03-07
Period period = Period.between(firstDate, secondDate);
System.out.println(period); //P4Y9M18D

Upvotes: 1

Related Questions