Month difference between dates

Discussion in 'Java' started by zamba, Mar 30, 2007.

  1. zamba

    zamba Guest

    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
     
    zamba, Mar 30, 2007
    #1
    1. Advertising

  2. zamba

    Mark Rafn Guest

    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/>
     
    Mark Rafn, Mar 30, 2007
    #2
    1. Advertising

  3. zamba

    Guest

    On Mar 30, 4:33 pm, "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..
    >
    > 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
     
    , Mar 30, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,387
  2. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    880
    Rob Schieber
    Sep 30, 2005
  3. Gustavo Niemeyer
    Replies:
    0
    Views:
    554
    Gustavo Niemeyer
    Sep 15, 2003
  4. Replies:
    0
    Views:
    428
  5. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    209
    Mark Schupp
    Aug 9, 2004
Loading...

Share This Page