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 1,503 times
3
-
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
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