Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ChronoUnit.DAYS.betweenChronoUnit.MONTHS.between will help you.

For some reason I read that you want a number of days.
 
Fred Victa
Ranch Hand
Posts: 224
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:ChronoUnit.DAYS.betweenChronoUnit.MONTHS.between will help you.

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
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came up with a solution. Here it is:


 
Marshal
Posts: 65005
246
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use Calendar. A dreadful class to use. Use LocalDate←link. You can read about the new date classes in the Java™ Tutorials.
 
Bartender
Posts: 1679
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!