0

How to calculate the number of "full" months between two dates with joda time, dropping incomplete months? For example, I have 2 dates

LocalDate from = new LocalDate (2018, 9, 10);
LocalDate to = new LocalDate (2018, 11, 15);

Between these dates there is one "full" month - October from 1 to 31. So I want to get is the number "1" by dropping the "incomplete" months - September and November

I need something like this

System.out.println(Months.monthsBetween(from, to).getMonths()); // returns 2
System.out.println(Months.**completed**MonthsBetween(from, to).getMonths()); // returns 1

UPD 1. I could achieve what I want as follows:

LocalDate from = new LocalDate (2018, 9, 10);
LocalDate to = new LocalDate (2018, 11, 15);

if (to.getDayOfMonth() != 1)
    from = from.plusMonths(1).withDayOfMonth(1);
if (to.getDayOfMonth() != 1)
    to = to.withDayOfMonth(1);

System.out.println(Months.monthsBetween(from, to).getMonths());

but maybe there is an out of the box method?

  • So you want the start of the next month after `from` to the start of the `to` date. – Peter Lawrey Sep 07 '18 at 10:48
  • 1
    I highly recommend you use the JSR 310 library built-in for Java 8 and available for Java 6. You will get much better support for it as well as it largely replaces jodatime – Peter Lawrey Sep 07 '18 at 10:55
  • https://stackoverflow.com/questions/34773160/joda-time-difference-in-months-between-two-dates –  Sep 07 '18 at 11:01
  • I’m not a Joda-Time user, but I’d be surprised if there was an out-of-the box method. There are very many ways to count months, it would be unreasonable to put them all in the library. So I suggest that something like what you are doing is necessary. – Ole V.V. Sep 07 '18 at 11:30
  • Sorry to ask, but did my answer helped u somehow – Eugene Sep 11 '18 at 21:44

2 Answers2

0

Wouldn't this means that you simply want to remove one month from each interval and do a difference between them?

    LocalDate fromMinusOne = from.minus(Months.ONE);
    LocalDate toMinusOne = to.minus(Months.ONE);

    System.out.println(Months.monthsBetween(fromMinusOne, toMinusOne).getMonths());
Eugene
  • 117,005
  • 15
  • 201
  • 306
  • No, I want to drop the first and last month, if it does not start from the first day of the month, and count the remaining number of full months – Andrey Beloborodov Sep 07 '18 at 11:16
0

Joda Time provides methods to extract days, months and years between two dates. Create two instances of you date.

DateTime startDate = DateTime.parse("1970-01-01", DateTimeFormat.forPattern("yyyy-MM-dd"))
DateTime endDate = DateTime.parse("2015-02-25", DateTimeFormat.forPattern("yyyy-MM-dd"))

You can create your date instances in LocalDate as well instead of DateTime.

Now, complete months between above two dates can be found easily with,

int months = Months.monthsBetween(startDate.withDayOfMonth(1), endDate.withDayOfMonth(1)).getMonths()

For days,

int days = Days.daysBetween(startDate, endDate).getDays()

Difference between two dates in months

For years,

int years = Years.yearsBetween(startDate, endDate).getYears();
jack jay
  • 2,493
  • 1
  • 14
  • 27