I'm using below code to calculate No of Years, Months and days between two dates using Joda-Time
public void getDateDiff(View view) {
DateTimeFormatter formatter = DateTimeFormat.forPattern("dd/MM/yyyy").withLocale(Locale.US);
DateTime startDate = null;
DateTime endDate = null;
// expected output 23...2....29
try {
startDate = formatter.parseDateTime("01/09/1995");
endDate = formatter.parseDateTime("30/11/2018");
Period period = new Period(startDate, endDate);
Log.e("No Of Years : ", period.getYears() + " Years, ");
Log.e("No Of Months : ", period.getMonths() + " Months, ");
Log.e("No Of Days : ", period.getDays() + " Days, ");
tvResult.setText(String.format("No Of Years : %d Years, \nNo Of Months : %d Months, \nNo Of Days : %d Days, ", period.getYears(), period.getMonths(), period.getDays()));
} catch (Exception e) {
e.printStackTrace();
}
}
The expected output of above code is( checked using this site and using one app in google play store)
23 years, 2 months, 29 days
but I'm getting below result using same date
/goku.com.demo E/No Of Years :: 23 Years,
/goku.com.demo E/No Of Months :: 2 Months,
/goku.com.demo E/No Of Days :: 1 Days,
Can any one help me to find issue what I'm missing in above code
I'm using using below
joda-time
Dependency.
implementation 'joda-time:joda-time:2.9.9'
P.S. already checked below links
- Getting the exact years or days or months between two joda date
- How to calculate difference between two dates in years...etc with Joda-Time
If need more information please do let me know. Thanks in advance. Your efforts will be appreciated.