• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Date differences in months...

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question: How can I get the number of months between 2 dates? ie. I want to find out how many months are between the following dates:
Aug 5, 01 - Aug 7, 01 : # of months = 1
Aug 28, 01 - Oct 2, 01 : # of months = 3 (Aug, Sept, Oct)
Aug 19, 00 - Aug 3, 01 : # of months = 13
I get the dates in string format, but convert them to java.sql.Dates using the code below:
uDate = ( java.util.Date )fDate.parse( fromDate, pos1 );
rSqlFromDate = new java.sql.Date(uDate.getTime());
uDate = ( java.util.Date )fDate.parse( toDate, pos2 );
rSqlToDate = new java.sql.Date(uDate.getTime());
...once I have them converted to sql.Dates I am able to use them in my queries, but I need to find out how many months are covered. I've never done this before... any help would be appreciated.
Thanks.
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need something like the java.util.Calendar class. Check the API for it's various methods.

Bosun
------------------
Bosun
SCJP for the Java� 2 Platform
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!