• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculate months between two dates  RSS feed

 
Neha Sud
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I wish to calculate months between two dates as written in code below


Could anyone explain this output or indicate if there is any error since no. of months should be 19 months.  along with certain no. of days

[code
 
Stephan van Hulst
Saloon Keeper
Posts: 7975
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getMonths() just returns the month field of the period. It also has a year field, which will be one. If you add a year's worth of months to 7, you'll get the expected 19.

You could use Period.toTotalMonths() to get the total amount of months in the period, but instead you might want to use localDate.until(now, ChronoUnit.MONTHS), to bypass the Period class completely.
 
Neha Sud
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Stephan for clear and useful information!!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!