The moose likes JSP and the fly likes Date differences in months... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Date differences in months..." Watch "Date differences in months..." New topic
Author

Date differences in months...

George Larry
Ranch Hand

Joined: Nov 07, 2001
Posts: 52
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.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date differences in months...
 
Similar Threads
Prepared Statement and Date class - Problem still exists
Date comparison
How toconvert a String into date Object
SQL Dates
Conversion