6

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.
  • 81,772
  • 15
  • 137
  • 161
Nevo Mashiach
  • 79
  • 1
  • 4

2 Answers2

14

Without more details, the standard way would be:

long months = ChronoUnit.MONTHS.between(odt1, odt2);
assylias
  • 321,522
  • 82
  • 660
  • 783
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,849
  • 5
  • 33
  • 47