Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Month difference between dates

Reply
Thread Tools

Month difference between dates

 
 
zamba
Guest
Posts: n/a
 
      03-30-2007
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

 
Reply With Quote
 
 
 
 
Mark Rafn
Guest
Posts: n/a
 
      03-30-2007
zamba <> wrote:
>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 <http://www.dagon.net/>

 
Reply With Quote
 
 
 
 
CodeForTea@gmail.com
Guest
Posts: n/a
 
      03-30-2007
On Mar 30, 4:33 pm, "zamba" <CRISTIAN_FERR...@HOTMAIL.COM> wrote:
> 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, ;
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM
Re: datetime: How to get diff between 2 dates in month units? Gustavo Niemeyer Python 0 09-15-2003 12:06 AM
datetime: How to get diff between 2 dates in month units? python@sarcastic-horse.com Python 0 09-12-2003 08:19 PM



Advertisments