Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Devaka Cooray
• Liutauras Vilda
• Jeanne Boyarsky
• Bear Bibeault
Sheriffs:
• Paul Clapham
• Knute Snortum
• Rob Spoor
Saloon Keepers:
• Tim Moores
• Ron McLeod
• Piet Souris
• Stephan van Hulst
• Carey Brown
Bartenders:
• Tim Holloway
• Frits Walraven
• Ganesh Patekar

# Calculate number of months between two dates

Ranch Hand
Posts: 224
1
Suppose someone's birth date is 8/29/2016. How can I create a method that calculates the number of full months between 8/29/2016 and today's date which is 8/20/2017? For this example, the method should return 11 months since 8/29/2017 is in the future.

Bartender
Posts: 2219
47
• 1

For some reason I read that you want a number of days.

Fred Victa
Ranch Hand
Posts: 224
1

For some reason I read that you want a number of days.

I'm sorry. I meant calculate a person's age in terms of the number of months. For example, if someone was born on 8/21/2015 and today's date is 8/21/2017, then he is 24 months old.

lowercase baba
Posts: 12753
51
The first step is to write out how YOU would calculate it by hand.  An example is always nice, but it isn't a spec or an algorithm.

So if I were born on Jan 2, 1975, how would you calculate my age in months?

Marshal
Posts: 6943
471
Without thinking about very clever ways (if there are such), have look at the LocalDate class. Task supposed to be quite simple.

Fred Victa
Ranch Hand
Posts: 224
1
I came up with a solution. Here it is:

Marshal
Posts: 65005
246
• 1

Bartender
Posts: 1679
17
• 1

Campbell Ritchie wrote:Don't use Calendar. A dreadful class to use. Use LocalDate←link. You can read about the new date classes in the Java™ Tutorials.

Agreed!

The java 8 LocalDate API is amazing. Check out all the examples online.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.