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?
Asked
Active
Viewed 3,867 times
6
-
1what is your sample input + expected output data? – luk2302 Apr 30 '18 at 09:02
-
How hard did you search? Might you for example have found [this answer](https://stackoverflow.com/a/26954864/5772882)? – Ole V.V. Apr 30 '18 at 10:38
2 Answers
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