3

I am using java 8 and I'm trying to calculate the amount of months between two OffsetDateTime objects. What is the best way to do this?

Ole V.V.
  • 76,217
  • 14
  • 120
  • 142

2 Answers2

7

Without more details, the standard way would be:

long months = ChronoUnit.MONTHS.between(odt1, odt2);
assylias
  • 310,138
  • 72
  • 642
  • 762
4

the most comprehensible way (IMO) is to use ChronoUnit

        OffsetDateTime odt1 = OffsetDateTime.now();
        OffsetDateTime odt2 = odt1.plusMonths(10);
        System.out.println(ChronoUnit.MONTHS.between(odt1, odt2));
Sharon Ben Asher
  • 13,127
  • 5
  • 31
  • 44