Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Month difference between dates

Thread view: 
zamba - 30 Mar 2007 21:33 GMT
Hi i want to know if there is a way to know exactly the number of
months (instead o days) between to diferent dates..

Thanks a lot
Mark Rafn - 30 Mar 2007 22:09 GMT
>Hi i want to know if there is a way to know exactly the number of
>months (instead o days) between to diferent dates..

I sometimes ask this as an interview question, and the only good answer is
to ask me questions about what exactly I want.  I'll do the same: please
specify what you mean by "number of months between two dates".

How many months are between Jan 1 and Jan 2?  Jan 1 and Jan 30?  Jan 1 2007
and Dec 31 2006?  Feb 1 and 28 days afterward in 2007?  in 2008?

For most reasonable definitions, the java.util.Calendar class will get you the
information you need to calculate the answer.
--
Mark Rafn    dagon@dagon.net    <http://www.dagon.net/>
CodeForTea@gmail.com - 30 Mar 2007 22:17 GMT
> Hi i want to know if there is a way to know exactly the number of
> months (instead o days) between to diferent dates..
>
> Thanks a lot

      Calendar calendar1 = new GregorianCalendar();
      Date trialTime = new Date();
      calendar1.setTime(trialTime);
      int month1 = calendar1.get(Calendar.MONTH);
      Date monthAhead = new Date();
      Calendar calendar2 = new GregorianCalendar();
      calendar2.setTime(monthAhead);
      calendar2.roll(Calendar.MONTH, 8);
      int month2 = calendar2.get(Calendar.MONTH);
      System.out.println("Month 1  = " + month1 );
      System.out.println("Month 2  = " + month2 );

Month 1  = 2
Month 2  = 10

Once you have the months from both dates, you can manipulate them.

Dinesh


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.