0

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

I googled, not able to find it.

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
pandiaraj
  • 580
  • 1
  • 6
  • 18
  • What you need to do exactly? Get the period between different dates. – Ravindra Ranwala Jun 07 '18 at 15:31
  • When Google has such an extraordinarily poor day there is no other resort than [RTFM, read the friendly manual](https://docs.oracle.com/javase/9/docs/api/java/time/Period.html) (a less nice interpretation of the abbreviation also exists). [Tutorial section](https://docs.oracle.com/javase/tutorial/datetime/iso/period.html) – Ole V.V. Jun 08 '18 at 11:53

2 Answers2

1

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
pandiaraj
  • 580
  • 1
  • 6
  • 18
0

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.

Ravindra Ranwala
  • 20,744
  • 6
  • 45
  • 63
  • I want only Years, Months and Days that's Period. I Understood the concepts of Duration and Period concept. Thanks – pandiaraj Jun 07 '18 at 19:36