Query regarding calculate months but with some conditions.
with Joda Date-time or date util.
Start Date : 01/01/2018
End Date : 31/12/2020
Total Period difference between above date: 36 months and 0 days
so total month =36
Start Date : 01/01/2018
End Date : 02/01/2021
Total Period difference between above date: 36 months and 2 days.
if there are days remaining then it consider single month.
so total month 36+1= 37
Date issueDate1=03/06/2017;
Date dateTo1=02/06/2020;
int investmentPeriod = Months.monthsBetween(issueDate1, dateTo1).getMonths();
By joda above months are coming 35 which is wrong.
Date start=23/06/2017;
Date end=06/07/2017;
here difference less than a month . so it consider as single month.
解决方案
ZoneId defaultZoneId = ZoneId.systemDefault();
String issueDate1="01/01/2017";
Date issueDate2=new SimpleDateFormat("dd/MM/yyyy").parse(issueDate1);
String dateTo1="31/12/2018";
Date dateTo2=new SimpleDateFormat("dd/MM/yyyy").parse(dateTo1);
here year month days can find easily.This giving ans of all question.
Instant instant = issueDate2.toInstant();
LocalDate localDatestart = instant.atZone(defaultZoneId).toLocalDate();
Instant instant1 = dateTo2.toInstant();
LocalDate localDateend = instant1.atZone(defaultZoneId).toLocalDate().plusDays(1);
Period diff = Period.between(localDatestart, localDateend);
System.out.printf("\nDifference is %d years, %d months and %d days old\n\n",
diff.getYears(), diff.getMonths(), diff.getDays());