- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get days, months and years between two Java LocalDate?
Set the two Java dates:
LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29);
Now, get the difference between two dates with Period class between() method:
Period p = Period.between(date1, date2);
Now, get the years, month and days:
p.getYears() p.getMonths() p.getDays()
Example
import java.time.LocalDate; import java.time.Period; public class Demo { public static void main(String[] args) { LocalDate date1 = LocalDate.of(2019, 3, 25); LocalDate date2 = LocalDate.of(2019, 4, 29); System.out.println("Date 1 = "+date1); System.out.println("Date 2 = "+date2); Period p = Period.between(date1, date2); System.out.println("Period = "+p); System.out.println("Years (Difference) = "+p.getYears()); System.out.println("Month (Difference) = "+p.getMonths()); System.out.println("Days (Difference) = "+p.getDays()); } }
Output
Date 1 = 2019-03-25 Date 2 = 2019-04-29 Period = P1M4D Years (Difference) = 0 Month (Difference) = 1 Days (Difference) = 4
- Related Articles
- Calculate the difference between two dates in days, weeks, months and years in Excel
- Converting days into years months and weeks - JavaScript
- Convert:a. 60 hours into days and hoursb. 50 months into years and months
- How Add or Subtract Specific Years, Months and Days to a Date in Excel?
- How to Convert Months to Years and Months in Excel?
- How to Add the Number of Years Months and Days to a Date in Google Sheets?
- How to count days between two dates in Java
- Add the following:7 years 8 months and 8 years 9 months
- LocalDate get() method in Java
- How can we create a MySQL function to find out the duration of years, months, days, hours, minutes and seconds?
- How to get the number of days between two Dates in JavaScript?
- C program to convert days into months and number of days
- MySQL query to get next closest day between two days?
- Get the last days of all the months in MySQL?
- MySQL update datetime column values and add 10 years 3 months 22 days and 10 hours, 30 minutes to existing data?

Advertisements