Difference between two dates in month in java

百般思念 提交于 2019-12-23 06:40:33

问题


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.


回答1:


 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());



回答2:


With Java Util Date the solution could like this...

final Date start = Date.from(ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC")).toInstant());
final Date end = Date.from(ZonedDateTime.of(2021, 1, 2, 0, 0, 0, 0, ZoneId.of("UTC")).toInstant());

final Calendar startCal = new GregorianCalendar();
startCal.setTime(start);

final Calendar endCal = new GregorianCalendar();
endCal.setTime(end);

final int yearOffset = (endCal.get(Calendar.YEAR) - startCal.get(Calendar.YEAR)) * 12;
final int monthOffset = endCal.get(Calendar.MONTH) - startCal.get(Calendar.MONTH);
final int dayOffset = (endCal.get(Calendar.DAY_OF_MONTH) - startCal.get(Calendar.DAY_OF_MONTH)) > 0 ? 1 : 0;
final int offset = yearOffset + monthOffset + dayOffset;

(I used a ZonedDateTime for the consturction of the Date but you can use a timestamp or whatever of course)

With Java 8 you would use DateTimes and ChronoUnits to do the trick. The solution could look like this:

final ZonedDateTime start = ZonedDateTime.of(2018, 1, 1, 0, 0, 0, 0, ZoneId.of("UTC"));
final ZonedDateTime end = ZonedDateTime.of(2021, 1, 2, 0, 0, 0, 0, ZoneId.of("UTC"));

final long monthOffset = ChronoUnit.MONTHS.between(start, end);
final int dayOffset = (end.getDayOfMonth() - start.getDayOfMonth()) > 0 ? 1 : 0;
final long offset = monthOffset + dayOffset;


来源:https://stackoverflow.com/questions/51999145/difference-between-two-dates-in-month-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!